I am new to these forums and for the past two days have been experimenting with C++. I coded a simple calculator that would +, -, /, * two numbers however, I have added a section where you can +, -, /, * the result from the two numbers. I would like someone to look over this code and tell me what's going wrong? Thanks
// Test.cpp : Defines the entry point for the console application.
// This C++ code was created by Nathan Bullock
#include <iostream>
#include <string>
usingnamespace std;
int GetTitle()
{
cout <<"Calculator Version 1.0 created and developed by Nathan Bullock\n ";
}
int GetUserInput()
{
cout << "\nEnter a number: ";
int nValue;
cin >> nValue;
return nValue;
}
char GetMathematicalOperation()
{
cout << "\nEnter a valid operator (+,-,*,or /): ";
char chOperation;
cin >> chOperation;
return chOperation;
}
int CalculateResult(int nX, char chOperation, int nY)
{
if (chOperation=='+')
return nX + nY;
if (chOperation=='-')
return nX - nY;
if (chOperation=='*')
return nX * nY;
if (chOperation=='/')
return nX / nY;
return 0;
}
void PrintResult(int nResult)
{
cout << "\nYour result is: " << nResult << endl;
cout << "\nWould you like to do anything else with " << nResult << " ?" << endl;
int choiceOne_Path;
cout << "\n\t >> Enter '1' Yes" << endl;
cout << "\t >> Enter '2' No\n" << endl;
cout << "\nEnter your choice: \n ";
cin >> choiceOne_Path;
if(choiceOne_Path == 1);
cout << "What would you like to do with " << nResult << " ?" << endl;
}
char GetInput()
{
cout << "\nEnter a valid operator (+,-,*,or /): ";
char DoTheSum;
cin >> DoTheSum;
return DoTheSum;
}
int GetUserInput3()
{
cout << "\nEnter a number: ";
int nValue2;
cin >> nValue2;
return nValue2;
}
int CalculateResult2(int nResult, char DoTheSum, int nValue2)
{
if (DoTheSum=='+')
return nResult + nValue2;
if (DoTheSum=='-')
return nResult - nValue2;
if (DoTheSum=='*')
return nResult * nValue2;
if (DoTheSum=='/')
return nResult / nValue2;
return 0;
}
void PrintResult2(int nResult)
{
cout << "\nYour result is: " << nResult << endl;
}
int main()
{
//Title
int title = GetTitle();
// Get first number from user
int nInput1 = GetUserInput();
// Get operation from user
char chOperator = GetMathematicalOperation();
// Get second number from user
int nInput2 = GetUserInput();
// Calculate result
int nResult = CalculateResult(nInput1, chOperator, nInput2);
// Print result
PrintResult(nResult);
// Would you like to do anything else to your answer?
char SecondTime = GetInput();
// Get users number again
int nInput3 = GetUserInput3();
int DoTheSum = CalculateResult2(int nResult, char DoTheSum, int nValue2);
int PrintResult2(nResult);
system ("PAUSE");
}
line 120 - you don't need the data types in front of the arguments sent to the function. nValue2 is not a variable you've declared - I'm assuming you meant to send nInput3? Did you mean to ask the user for another operator before you do the calculation? You're sending DoTheSum as the operator, but DoTheSum has no value in main before you send it.
line 52 - remove the semicolon at the end of the if condition. Though really, I'd suggest removing this logic from the print function and just let the print function output the results.
Other suggestions...
The GetTitle function just outputs a line of text. It can be a void function since you don't return any value, and you don't need to create the int variable title in main since no value is being returned.
I might suggest implementing a menu of operations that you can return to. You could keep track of the current total in the calculator and add an option into the calculator menu for the user to clear that value in memory if they want to start a new series of operations. I would try to avoid having multiple functions that basically do the same thing - e.g. have one calculate function that takes an operator and two operands, have one print function, have one get input function etc.