I've searched around here and on the internet for some solutions to this, but I can't seem to get it figured out. Basically, I am writing a very, very simple calculator-type program. As you'll see in my code I have it setup to give the user 4 options for operators, then it asks for two numbers, completes the operation and outputs, then prompts for another problem.
The problem I keep finding myself with is that if the user inputs a char instead of an integer the program infinitely loops. The problem only happens, however, if the user goes through the program twice.
eg.. I choose 1 for addtion, input 2 and 2. The program outputs 4 and asks if I have another problem. I input 1 for yes, then I input "a". *infinite loop*
This is my first program that I've ever tried on my own. I'm not using the best book (C++ for Dummies.... ) and I'm sure my code will receive a fair amount of criticism. But bring it on, please. I am open to any constructive things you can say.
Thanks
Austin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
//Beginning of the calculator application in C++.
//Start with statements and the initial options integer.
cout << "Welcome to Austin's Calculator!"
<< "\n===============================" << endl;
cout << "At this time it is designed to perform \nbasic "
<< "mathematic functions to two numbers." << endl;
start:
cout << "\n\nWhich function would you like to perform?" << endl;
cout << "\n\n\nPress 1 for Addition."
<< "\nPress 2 for Subtraction."
<< "\nPress 3 for Multiplication."
<< "\nPress 4 for Division.\n" << endl;
cout << "Please enter your selection: ";
int sel;
cin >> sel;
if (sel == 1)
{
double xadd;
double yadd;
cout << "\nEnter your first number: ";
cin >> xadd;
cout << "Enter your second number: ";
cin >> yadd;
cout << "\nThe answer is: " << xadd + yadd << endl;
}
else if (sel == 2)
{
double xsub;
double ysub;
cout << "\nEnter your first number: ";
cin >> xsub;
cout << "Enter your second number: ";
cin >> ysub;
cout << "\nThe answer is: " << xsub - ysub << endl;
}
else if (sel == 3)
{
double xmul;
double ymul;
cout << "\nEnter your first number: ";
cin >> xmul;
cout << "Enter your second number: ";
cin >> ymul;
cout << "\nThe answer is: " << xmul * ymul << endl;
}
else if (sel == 4)
{
double xdiv;
double ydiv;
cout << "\nEnter your first number: ";
cin >> xdiv;
cout << "Enter your second number: ";
cin >> ydiv;
cout << "\nThe answer is: " << xdiv / ydiv << endl;
}
else if (sel != 1,2,3,4)
{
goto end;
}
//Integer for Repeat represented by "rep"
int rep;
cout << "\n Would you like to perform another operation?" << endl;
cout << "\nPress 1 for Yes." << endl;
cout << "Press 2 for No." << endl;
cout << "\nPlease enter your selection: ";
cin >> rep;
if (rep == 1)
{
goto start;
}
else if (rep == 2)
{
goto end;
}
else
{
goto end;
}
end:
cout << "\n\nThank you for using Austin's Calculator!" << endl;
// Wait until ready to close the program.
system("PAUSE");
return 0;
}
|