Simple question about loop while
Oct 7, 2011 at 1:48pm UTC
Hi guys,
I literally only started messing about with C++ yesterday.
After watching some tutorial videos and looking at other peoples code i've made a simple calculator, probably similar to lots made before. The only problem I'm having is, that when either option 2. or 3. is selected and the calculation is complete, is doesn't bring up the menu again to select which function to carry out next, whereas it does with options 1. and 4.
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 106 107 108 109 110 111 112
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
while (true )
{
signed long int num1, num2;
signed long int ans;
cout <<"Dickos Simple Calculator" << endl;
cout << endl;
cout <<"1. Addition" << endl;
cout <<"2. Subtraction" << endl;
cout <<"3. Division" << endl;
cout <<"4. Multiplication" << endl;
cout <<"5. Exit program" << endl;
cout << endl << endl;
cout << endl << endl;
cout <<"Please enter a number from the menu above <1-5>: " ;
cin >> ans;
cout << endl;
if ( ans == 1 )
{
cout << "You have selected the Addition Calculator" << endl << endl;
cout << endl;
cout << "Please enter first number: " ;
cin >> num1;
cout << endl;
cout <<"Please enter second number: " ;
cin >> num2;
cout << endl;
cout <<"The answer is: " << num1 + num2 << endl;
cout << endl << endl;
cout << endl << endl;
}
if ( ans == 2 )
{
cout << "You have selected the subtraction calculator" << endl << endl;
cout << endl;
cout <<"Please enter the first number: " ;
cin >> num1;
cout << endl;
cout <<"Please enter the second number: " ;
cin >> num2;
cout << endl << endl << endl;
cout <<"The answer is: " << num1 - num2 << endl;
cin >> ans;
cout << endl << endl;
cout << endl << endl;
}
if ( ans == 3 )
{
cout << "You have selected the division calculator" ;
cout << endl << endl;
cout <<"Please enter the first number: " ;
cin >> num1;
cout << endl << endl;
cout <<"Please enter the seond number: " ;
cin >> num2;
cout << endl << endl;
cout <<"The answer is: " << num1 / num2 << endl;
cin >> ans;
cout << endl << endl;
cout << endl << endl;
}
if (ans == 4)
{
cout<< endl;
cout<<"(Simple Division) " << endl;
cout<<"Please enter first number: " ;
cin >> num1;
cout<<"Please enter second number: " ;
cin >> num2;
cout << endl;
cout <<"The answer is: " << num1 / num2 << endl;
cout << endl << endl;
cout << endl << endl;
}
if (ans == 5)
{
cout << endl;
cout << endl;
cout<< "Thank You! " ;
exit (1);
}
}
system("pause" );
return 0;
}
I'm sure this will be an easy question for many people here, thanks in advance :)
Oct 7, 2011 at 1:51pm UTC
Line 57 and 78 are read statements instead of print statements. You probably meant to write cout << ans?
Oct 7, 2011 at 1:57pm UTC
Ah, well spotted thanks :)
I've actually just removed them lines of code. I was getting mixed up between the 'int ans' for the calculation selection and the answer of the actual calculation which didn't even need to be cout.
Thanks very much.
Topic archived. No new replies allowed.