switch (i)
{
case '+': result = addition (firstnum, secondnum), cout << "Result: " << result << endl;
break;
case '-': result = subtraction (firstnum, secondnum), cout << "Result: " << result << endl;
break;
case '*': result = multiplication (firstnum, secondnum), cout << "Result: " << result << endl;
break;
case '/': result = divition (firstnum, secondnum), cout << "Result: " << result << endl;
break;
default: cout << "Calculations can't be made, try again" << endl;
}
cout << "To use old result type r, to start a new expression type n, and to exit calculator type e." << endl;
cin >> jared;
while (jared == 'r')
{
firstnum = result;
cout << "Here is your previous result:" << firstnum << endl;
cout << "Enter symbol (+, -, *, /): " << endl;
cin >> i;
cout << "Enter second number: " << endl;
cin >> secondnum;
switch (i)
{
case '+': result = addition (firstnum, secondnum), cout << "The result is: " << result << endl;
break;
case '-': result = subtraction (firstnum, secondnum), cout << "The result is: " << result << endl;
break;
case '*': result = multiplication (firstnum, secondnum), cout << "The result is: " << result << endl;
break;
case '/': result = divition (firstnum, secondnum), cout << "The result is: " << result << endl;
break;
default: "Calculations can't be made, try again";
}
cout << "To use old result type r, to start a new expression type n, and to exit calculator type e." << endl;
cin >> jared;
}
if (jared == 'n') goto loop;
if (jared == 'e')
{
cout << "Exiting calculator";
}
cin >> i;
return 0;
}
the problem is the when the user inputs a name or anything that is not a number, it crashes. I have no idea on how to fix this.
Sanitise the input. You're allowing the user to enter bad data.
There are many ways to do this. For example, take the input as a string, and then check it. If it's a number, turn it into an int and carry on. If not, reject it.
It definitely doesn't crash for me, but instead of the loop:, why not make the program modular? and Numeri, I tried using the isdigit(firstnum) and it made the code non functional.
What do you mean it doesn't work rocker? It worked perfectly fine for me.
Enter first number:
5
Enter symbol (+, -, *, /):
+
Enter second number:
5
Result: 10
To use old result type r, to start a new expression type n, and to exit calculator type e.
e
Exiting Calculator
2
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}
int multiplication (int a, int b)
{
int r;
r=a*b;
return (r);
}
int divition (int a, int b)
{
int r;
r=a/b;
return (r);
}
int _tmain(int argc, _TCHAR* argv[])
{
int firstnum, secondnum;
char i;
string input = "";
bool rightnum = true;
bool rightsym = true;
bool secondrightnum = true;
int result;
while (rightnum)
{
cout << "Enter first number:" << endl;
getline(cin, input);
stringstream str(input);
if (str >> firstnum)
{
rightnum = false;
}
else
{
cout << "Invalid number, please try again." << endl;
}// end of first loop
}
while (rightsym)
{
cout << "Enter symbol (+, -, *, /)" << endl;
cin >> i;
if (i == '+')
rightsym = false;
else if (i == '-')
rightsym = false;
else if (i == '*')
rightsym = false;
else if (i == '/')
rightsym = false;
else
cout << "Invalid symbol, please try again." << endl;
}// end of second loop
while (secondrightnum)
{
cout << "Enter second number:" << endl;
getline(cin, input);
stringstream str(input);
if (str >> secondnum)
{
secondrightnum = false;
}
else
{
cout << "Invalid number, please try again." << endl;
}
}// end of third loop
switch(i)
{
case '+': result = addition (firstnum, secondnum), cout << "The result is: " << endl;
break;
case '-': result = subtraction (firstnum, secondnum), cout << "The result is: " << endl;
break;
case '*': result = multiplication (firstnum, secondnum), cout << "The result is: " << endl;
break;
case '/': result = divition (firstnum, secondnum), cout << "The result is: " << endl;
default: cout << "Calculation can't be made, please try again." << endl;
}
cin >> i;
return 0;
}
i changed my code. but now what it does is skip the part of asking the user input for the second number. also it is not making the calculation in the switch statement. a little help.
I changed it because this way is better, because in this code, the code is trying to convert a string to a number. when the code confirms that the string is not a number, it will loop until the user gives a number. The only problem that i am having with this code is that when the user is supposed to input the second number, the code jumps it and then it asks again, as if the user already gave a number.
cin >> i;: This line of code does not extract the new line character from the buffer. getline(cin, input); extracts from the buffer until it hits the new line character, and also extracts it.
Just put cin.ignore(80, '\n'); after your cin >> i;.
Also, if you want your text to be formatted as code, type "code" before it and "/code" after it. You have to replace those quotes with brackets for it to work. Or just use the <> button in the format section.