Check Code for Calculator

March 12, 2020 at 08:46:14
Specs: Windows 10
Hello, i use Visual Studio for C++. I am relatively new.
My code is for making a calculator and compiles but produces a random number along with the result.

Calculator.cpp
#include <iostream>
#include "ForwardDeclaration.h"

int main()
{
//Get first number from user
int value_1{ getUserInput() };

//Get mathematical operation from user
char symbol{ getMathematicalOperation() };

//Get second number from user
int value_2{ getUserInput() };

//Calculate result
std::cout << value_1 << ' ' << symbol << ' ' << value_2 << ' = ' << calculateResult(value_1, symbol, value_2)<< '\n';

return 0;
}

ForwardDeclaration.h

#ifndef header
#define header
int getUserInput();
char getMathematicalOperation();
int calculateResult(int value_1, char symbol, int value_2);

#endif

Function.cpp

#include <iostream>

int getUserInput()
{
std::cout << "Enter an integer ";
int input{ 0 };
std::cin >> input;

return input;
}

char getMathematicalOperation()
{
std::cout << "Enter a valid symbol such as'+', or '-', or '/', or '*'\n";
char symbol;
std::cin >> symbol;

return symbol;
}


int calculateResult(int value_1, char symbol, int value_2)
{
if ( symbol == '+') {
return value_1 + value_2;
}
else if (symbol == '-') {
return value_1 - value_2;
}
else if (symbol == '*') {
return value_1 * value_2;
}
else if (symbol == '/') {
return value_1 / value_2;
}
else {
std::cout << "Something wrong went with your operation\n";

return 0;
}
}


See More: Check Code for Calculator


#1
March 12, 2020 at 11:31:39
Try single-step ping through the code in the debugger to see where it is going wrong.

Report •
Related Solutions


Ask Question