Cmptr_Prgrmr2B
01-19-2009, 09:35 PM
Hi,
I just started learning C++ and decided to make a basic program that solved adddition, subtraction, multiplication, and division problems. I ran into quite a few errors, but eventually worked them all out except for one. My program is working fine until my dad, a math major, decides to try to divide by 0, which we all know is completely impossible. My program has a complete and total meltdown and gives me a error message saying that is must close. So naturally I try to put in an if statement to catch the variable when it equals 0 to stop it from dividing. That worked, but then i tried to put in a while loop so that if the variable equaled 0 it would go back to the menu where the user would select which option they would like to do. That is the part that is completely and totally messing up. I've got it to where it will go back to the menu, but when i try to enter a new value it still thinks it that the variable equals 0. Can someone possible look at my code and see what my problem is?
Thanks,
Cmptr_Prgrmr2B
Here is my code:
#include <iostream>
using namespace std;
int main ()
{
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o;
{cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n Enter your choice number here:";
cin>> b;
cin.ignore ();}
if (b == 1) {cout<< "Addition then, very well!\n Input numbers to be added:\n";
cin>> c; cin>> d;
cin.ignore ();
e = c + d;
cout<< "Your answer is:"; cout<< e; cout<< "\n";}
else if (b == 2) {cout<< "I've always loved subtraction!\n Input numbers to be subtracted, larger of the two first:\n";
cin>> f; cin>> g;
cin.ignore ();
h = f - g;
cout<< "Your answer is:"; cout<< h; cout<< "\n";}
else if (b == 3) {cout<< "Multiplication!! OH NO!!\n Input numbers to be multiplied:\n";
cin>> i; cin>> j;
cin.ignore ();
k = i * j;
cout<< "Your answer is:"; cout<< k; cout<< "\n";}
else if(b == 4) {cout<< "Division... so boring... \n Input numbers to be divided, dividend first:\n";}
cin>> l; cin>> m;
cin.ignore ();
while ( m == 0) {cout<< "Error, you cannot divide by zero!\n";cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n Enter your choice number here:";
cin>> b;
cin.ignore ();} //For some reason this loop isn't wanting to work. I also don't know the code to make something go back to a previous line.
if ( m != 0 ); { n = l / m;
cout<< "Your answers is:"; cout<< n; cout<< "\n";};
// Everthing else works fine except for the loop.
}
I just started learning C++ and decided to make a basic program that solved adddition, subtraction, multiplication, and division problems. I ran into quite a few errors, but eventually worked them all out except for one. My program is working fine until my dad, a math major, decides to try to divide by 0, which we all know is completely impossible. My program has a complete and total meltdown and gives me a error message saying that is must close. So naturally I try to put in an if statement to catch the variable when it equals 0 to stop it from dividing. That worked, but then i tried to put in a while loop so that if the variable equaled 0 it would go back to the menu where the user would select which option they would like to do. That is the part that is completely and totally messing up. I've got it to where it will go back to the menu, but when i try to enter a new value it still thinks it that the variable equals 0. Can someone possible look at my code and see what my problem is?
Thanks,
Cmptr_Prgrmr2B
Here is my code:
#include <iostream>
using namespace std;
int main ()
{
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o;
{cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n Enter your choice number here:";
cin>> b;
cin.ignore ();}
if (b == 1) {cout<< "Addition then, very well!\n Input numbers to be added:\n";
cin>> c; cin>> d;
cin.ignore ();
e = c + d;
cout<< "Your answer is:"; cout<< e; cout<< "\n";}
else if (b == 2) {cout<< "I've always loved subtraction!\n Input numbers to be subtracted, larger of the two first:\n";
cin>> f; cin>> g;
cin.ignore ();
h = f - g;
cout<< "Your answer is:"; cout<< h; cout<< "\n";}
else if (b == 3) {cout<< "Multiplication!! OH NO!!\n Input numbers to be multiplied:\n";
cin>> i; cin>> j;
cin.ignore ();
k = i * j;
cout<< "Your answer is:"; cout<< k; cout<< "\n";}
else if(b == 4) {cout<< "Division... so boring... \n Input numbers to be divided, dividend first:\n";}
cin>> l; cin>> m;
cin.ignore ();
while ( m == 0) {cout<< "Error, you cannot divide by zero!\n";cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n Enter your choice number here:";
cin>> b;
cin.ignore ();} //For some reason this loop isn't wanting to work. I also don't know the code to make something go back to a previous line.
if ( m != 0 ); { n = l / m;
cout<< "Your answers is:"; cout<< n; cout<< "\n";};
// Everthing else works fine except for the loop.
}