Question:
C++ code help- do... while loop- can't get it right...?
m a
2010-03-07 23:30:05 UTC
here's my code:
#include
#include
using namespace std;
int main ()
{
int n1, n2, result;
char selc, sign;
result=0;


do
{
cout<<"Enter one of the following values:"< cout< cout< cout< cout< cout< cout<<"Enter selection now: ";
cin>> selc;
switch (selc)
{
case '1':result= n1+n2;
sign='+';
break;
case '2': result= n1-n2;
sign= '-';
break;
case '3':result=n1*n2;
sign= '*';
break;
case '4': result=n1%n2;
sign= '%';
break;
case '9': return 1;
default: cout<<"Invalid selection, please try again."< }

cout<<"Enter the first number: ";
cin>>n1;
cout<<"Enter the second number: ";
cin>>n2;
cout<<"The result of "< }
while (selc != '9');


return 0;
}
the program works right, but when i come to do the math, its completely wrong, it gives me crazy numbers, anyone know how to fix this?
thanks in advance :)
Five answers:
Danny
2010-03-07 23:39:08 UTC
Try this:



#include

#include

using namespace std;

int main ()

{

int n1, n2, result;

char selc, sign;

result=0;





do

{

cout<<"Enter one of the following values:"<
cout<
cout<
cout<
cout<
cout<
cout<<"Enter selection now: ";

cin>> selc;



cout<<"Enter the first number: ";

cin>>n1;

cout<<"Enter the second number: ";

cin>>n2;



switch (selc)

{

case '1':result= n1+n2;

sign='+';

break;

case '2': result= n1-n2;

sign= '-';

break;

case '3':result=n1*n2;

sign= '*';

break;

case '4': result=n1%n2;

sign= '%';

break;

case '9': return 1;

default: cout<<"Invalid selection, please try again."<
}

cout<<"The result of "<
}

while (selc != '9');





return 0;

}





--------------------------------------------------------



The problem I think is that you're making it do the calculations BEFORE you ask the user for the two number choices, so it just uses random numbers instead. So what I've done is place the switch AFTER the number input.
smokky_says_relax
2010-03-07 23:46:44 UTC
The C++ program you have written is compiled line by line so you get the selection from the user before assigning a value to the integer variable n1 and n2 . Thus during operation n1 and n2 has garbage value or crazy numbers :P . Try shifting this code snipped before the first statement in do-while loop.

cout<<"Enter the first number: ";

cin>>n1;

cout<<"Enter the second number: ";

cin>>n2;



Now the values of n1 and n2 are assigned before the actual math operation takes place.



The modified do while loop will look like this -



do

{

//Getting inputs

cout<<"Enter the first number: ";

cin>>n1;

cout<<"Enter the second number: ";

cin>>n2;

//end

cout<<"Enter one of the following values:"<
cout<
cout<
cout<
cout<
cout<
cout<<"Enter selection now: ";

cin>> selc;

switch (selc)

{

case '1':result= n1+n2;

sign='+';

break;

case '2': result= n1-n2;

sign= '-';

break;

case '3':result=n1*n2;

sign= '*';

break;

case '4': result=n1%n2;

sign= '%';

break;

case '9': return 1;

default: cout<<"Invalid selection, please try again."<
}

cout<<"The result of "<
}

while (selc != '9');







Hope it helped :)
Jim
2010-03-08 00:04:52 UTC
instead of using numbers for a user interface for the program, perhaps you could input a character, like a + * - / q or Q or ESC (char 0x1b) to quit the program. having multiple ways to get out is always nice. people like flexibility. and making your programs more flexible might just land you a better grade. maybe.



I don't know what your prof's instructions were, whether they said to use those menu items or not, but frankly that menu system stinks. not user friendly.



and it's not "sign" it's "op" or "operator". since operator is a reserved word probably, use op., or better yet, command.



programs should return 0 on success. not 1 or more unless you wish to flag a program error or return a specific error code to the OS (the ERRORLEVEL in a batch file).



by the way, since when did you ever do anything like cin>>n1>>n2; ? you forgot to prompt for input for the 2 numbers. so the c compiler is simply using whatever random number happens to be in memory when the memory is allocated for n1 and n2 (you didn't initialize them!).



if you don't want that, you either initialize it or you input something into the variables. if you don't to initialize it, the compiler is going to give you a warning about them being uninitialized.
wal_tico2003
2010-03-07 23:36:50 UTC
It look good but, you have to ask for n1 and n2 before the 'do' too (yes, ask two times one outside the while and one inside). Because when the person choose the selection , the program work with trash (random hexadecimal values) in n1 and n2.
vmanes
2010-03-07 23:36:36 UTC
You're calculating a result before you get the input values from the user. You get an operation selection from the user, then do the operation in the switch, then you ask for two numbers. How's that gonna work?



You need to either ask for numbers before the operation choice, or in each switch case ask for the numbers.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...