PDA

View Full Version : C++ Programming help!


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.
}

tomws
01-19-2009, 11:01 PM
The variable m does still equal zero since you're don't change the value in the while loop. The only input you're taking is variable b.

Also, your curly braces are out of place for the division operation.

Regarding the comment in the code, you don't want to 'go back' to a previous line. You need to structure your code with a loop. Here's one possibility that adds in a flag value to allow an arbitrary number of operations.

/* ... */
int main ()

{
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o; // your variable names need work

cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n 0) Exit\n Enter your choice number here:"; /*added option 0*/
cin>> b;
cin.ignore ();

while (b != 0)
{
/*addition, subtraction, multiplication same as before*/
else if(b == 4)
{
cout<< "Division... so boring... \n Input numbers to be divided, dividend first:\n"; /*} out of place */
cin>> l; cin>> m;
cin.ignore ();
if (m == 0)
{
cout<< "Error, you cannot divide by zero!\n";
} // end if
else // if ( m != 0 )
{
n = l / m;
// cout<< "Your answers is:"; cout<< n; cout<< "\n";
/*easier to write your cout statements like this...*/
cout << "Your answer is: " << n << "\n";
} // end else
} // end else if (b==4)

/*run another operation*/
cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n 0) Exit\n Enter your choice number here:"; /*added option 0*/
cin>> b;
cin.ignore ();
} // end while


Give that a try. It ought to provide the looping ability you want and handle divide-by-zero errors, or else it will break and give you something else to debug. It's been a while since my C++ classes.

I'm not sure why you're using those cin.ignores.

Cmptr_Prgrmr2B
01-19-2009, 11:19 PM
I'm using the cin.ignores because the stuff i read told me that i needed to discard, or "ignore" the Enter function when i input a variable. Thanks for your help, though. I will try the new code immediately!

So, I tried the new coding and everything worked fine except for the loop. Is there just a couple of symbols that i missed? Or did i completely screw it up? I'll put the revised codes up so that someone can look at them.

#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 0) Exit \n Enter your choice number here:";
cin>> b;
cin.ignore ();

while (b != 0) {
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 ();
if ( m == 0) {cout<< "Error, you cannot divide by zero!\n";}

else ( m != 0 ); { n = l / m;
cout<< "Your answers is:"; cout<< n; cout<< "\n";};

} cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n 0) Exit \n Enter your choice number here:";
cin>> b;
cin.ignore ();}


// Everthing else works fine except for the loop. And i get the message of "You can not divide by 0"
// but it still trys to divide... did i just miss something?

tomws
01-20-2009, 02:38 AM
An important part of coding is creating human-readable code. It saves loads of time debugging if you follow practices like using indenting and white space to visually layout your code. I'll bet you could see the problem if your code were a little cleaner. Here's your exact program but laid out with common use of indenting and white space.

EDIT: Adding extra block comments for clarity.

#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 0) Exit \n E
nter your choice number here:";
cin>> b;
cin.ignore ();

while (b != 0)
{
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";
} // end: if (b == 1 )
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";
} // end: else if (b == 2)
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";
} // end: else if (b == 3)
/*fixme*/
else if (b == 4)
{
cout<< "Division... so boring... \n Input numbers to be divided, dividend first:\n";
} // end: else if (b == 4)
cin>> l;
cin>> m;
cin.ignore ();
if (m == 0)
{
cout<< "Error, you cannot divide by zero!\n";
} // end: if (m == 0)
else ( m != 0 ); // end: else ???

{
n = l / m;
cout<< "Your answers is:"; cout<< n; cout<< "\n";
}; // end: mystery block
} // end: while (b != 0)
cout<< "Please choose equation type:\n 1) Addition\n 2) Subtraction\n 3) Multiplication\n 4) Division\n 0) Exit \n Enter your choice number here:";
cin>> b;
cin.ignore ();
/*end fixme*/

} // end: main

The part between the /*fixme*/ comments needs work. By examining the structure of the division portion, you should be able to see that the logic doesn't work correctly as you've implemented it (which is also different from my previous suggestion). Read through it as the computer would during execution and you ought to be able to pick out why I've flagged that section.

Cmptr_Prgrmr2B
01-20-2009, 02:54 AM
I revised your code and it worked fine! Thank your for all your help!