By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

Equation printing program

P: 67
So i have a question on how i should start doing this... pretty new at c++

i have to ask the user for an integer, an operator, and then another integer to give an answer. But i also have to show the whole equation. I want to try to use loops to do this.
Sep 19 '07 #1
Share this Question
Share on Google+
63 Replies


Expert 10K+
P: 11,448
So i have a question on how i should start doing this... pretty new at c++

i have to ask the user for an integer, an operator, and then another integer to give an answer. But i also have to show the whole equation. I want to try to use loops to do this.
So you want to ask the user for all that input *repeatedly* don't you? Otherwise
I don't see any reason for a loop except if you want to make your program robust
and ask for correct input over and over again if the user makes a mistake.

kind regards,

Jos
Sep 19 '07 #2

P: 67
Well i want to ask the user to put in the integer and then the operator and then the next integer but not all at once. and then if they put 2 a + then a 3 i need it to say 2+3=5



So you want to ask the user for all that input *repeatedly* don't you? Otherwise
I don't see any reason for a loop except if you want to make your program robust
and ask for correct input over and over again if the user makes a mistake.

kind regards,

Jos
Sep 19 '07 #3

Expert 10K+
P: 11,448
Well i want to ask the user to put in the integer and then the operator and then the next integer but not all at once. and then if they put 2 a + then a 3 i need it to say 2+3=5
Well, let's start there then but it doesn't take a loop; pseudo code alert:

Expand|Select|Wrap|Line Numbers
  1. int a= askInteger("please give left operand");
  2. char op= askCharacter("please give one of +-*/");
  3. int b= askInteger("please give right operand");
  4.  
  5. int result;
  6. switch (op) {
  7.    // according to the value of op, assign the 
  8.    // correct value to variable 'result'
  9. }
  10. printf("%d%c%d=%d\n", a, op, b, result);
  11.  
All you have to do now is complete the two input functions and the content of
the switch statement.

kind regards,

Jos
Sep 19 '07 #4

P: 67
I don't quite understand how that works.

Well, let's start there then but it doesn't take a loop; pseudo code alert:

Expand|Select|Wrap|Line Numbers
  1. int a= askInteger("please give left operand");
  2. char op= askCharacter("please give one of +-*/");
  3. int b= askInteger("please give right operand");
  4.  
  5. int result;
  6. switch (op) {
  7.    // according to the value of op, assign the 
  8.    // correct value to variable 'result'
  9. }
  10. printf("%d%c%d=%d\n", a, op, b, result);
  11.  
All you have to do now is complete the two input functions and the content of
the switch statement.

kind regards,

Jos
Sep 19 '07 #5

P: 67
Anyone else have a simpler idea or can explain it for me.
Sep 19 '07 #6

kreagan
100+
P: 153
Anyone else have a simpler idea or can explain it for me.
What Jos is trying to say is: you don't need a loop (atleast how the problem was explained). If you want the user to insert 2 arguements and a mathematical operation (+ or - for example),

1.) ask for the first arguement
2.) ask for the second arguement
3.) ask for the mathematical expression.
4.) Use a switch statement to control to find result.
5.) Print out equation and result.

However, if you want the user to put in any number of arguments (1+1 - 3 = ?), you must do something different.
Sep 19 '07 #7

P: 67
This is what I started to do and i know its not gonna work.. What should i do from here???


Expand|Select|Wrap|Line Numbers
  1. int main()
  2.   int num1, num2, answer;
  3.   char operator;
  4.     cout<<  "Input Integer.\n";
  5.     cin>> num1; 
  6.  
  7.   do
  8.   { 
  9.     cout<< "Choose + for addition.\n"
  10.         << "Choose - for subtraction.\n"
  11.         << "Choose * for multiplication.\n"
  12.         << "Choose / for division.\n"
  13.         << "Enter choice and press Return; ";
  14.     cin>> operator;
  15.     cout<< "Input Second Integer";
  16.     cin>> num2;
  17.  
  18.  
  19.       switch (operator)
  20.        {
  21.      int answer;
  22.         case 1:
  23.  
  24.       num1+num2=answer;
  25.        cout<< " The Answer is: "<<num1+num2=answer<<".\n";
  26.            break; 
  27.  
  28.         case 2:
  29.  
  30.       num1-num2=answer;
  31.        cout<< " The Answer is: "<<num1-num2=answer<<".\n";
  32.            break;
  33.  
  34.         case 3:
  35.  
  36.       num1*num2=answer;
  37.        cout<< " The Answer is: "<<num1*num2=answer<<".\n";
  38.            break;
  39.  
  40.         case 4:
  41.  
  42.       num1/num2=answer;
  43.        cout<< " The Answer is: "<<num1/num2=answer<<".\n";
  44.            break;
  45.  
  46.         default:
  47.  
  48.           cout<< "This is not a correct operator.\n";
  49.           cout<< "Choose Again.\n";
  50.        }
  51.   }while (operator != +,-,*,/);
  52.  
  53.      return0;
  54. }
Sep 19 '07 #8

kreagan
100+
P: 153
This is what I started to do and i know its not gonna work.. What should i do from here???


int main()
{
int num1, num2, answer;
char operator;
cout<< "Input Integer.\n";
cin>> num1;

do
{
cout<< "Choose + for addition.\n"
<< "Choose - for subtraction.\n"
<< "Choose * for multiplication.\n"
<< "Choose / for division.\n"
<< "Enter choice and press Return; ";
cin>> operator;
cout<< "Input Second Integer";
cin>> num2;


switch (operator)
{
int answer;
case 1:

num1+num2=answer;
cout<< " The Answer is: "<<num1+num2=answer<<".\n";
break;
default:

cout<< "This is not a correct operator.\n";
cout<< "Choose Again.\n";
}
}while (operator != +,-,*,/);

return0;
}
it's okay. The logic is totally there but you just need to fix some syntax problems. Your switch statement checks if the user inputs "1, 2, 3, or 4". I would suggest reading Switch Statements since there an example that will directly help you.

Also, the expression (operator != +,-,*,/) is not correct. Right idea but not what you want. You need to compare a character to a character, then another character to another character. If I was to write it in words, it would be:

while operator != addition sign OR operator != subtraction sign OR ...

Furthermore, you can never choose again with the logic you are using (I don't know if you want to do that).
Sep 19 '07 #9

P: 67
Instead of case 1: would i put case '+': ?
and also should it be while ((operator != +) && (operator != -) &&....)
And the choose again part i didnt work on yet i was just putting it there to remind myself i had to do it.

is there any other suggestions you have?

it's okay. The logic is totally there but you just need to fix some syntax problems. Your switch statement checks if the user inputs "1, 2, 3, or 4". I would suggest reading Switch Statements since there an example that will directly help you.

Also, the expression (operator != +,-,*,/) is not correct. Right idea but not what you want. You need to compare a character to a character, then another character to another character. If I was to write it in words, it would be:

while operator != addition sign OR operator != subtraction sign OR ...

Furthermore, you can never choose again with the logic you are using (I don't know if you want to do that).
Sep 19 '07 #10

kreagan
100+
P: 153
Instead of case 1: would i put case '+': ?
and also should it be while ((operator != +) && (operator != -) &&....)
And the choose again part i didnt work on yet i was just putting it there to remind myself i had to do it.

is there any other suggestions you have?
try it out and see what happens. You will learn much more if you try everything you can think of before coming to us ;)
Sep 19 '07 #11

P: 67
if i have my equation set so that x+y=z and the user is inputing x and y
how do i get it to print out the numbers in place so that if he put in 5 and 6 it will say 5+6=11???
Sep 20 '07 #12

sicarie
Expert Mod 2.5K+
P: 4,677
try it out and see what happens. You will learn much more if you try everything you can think of before coming to us ;)
Did you try it yourself as kreagan suggested? What did you find? What are you confused with?
Sep 20 '07 #13

P: 67
Ok well i totally changed my code. I think I'm close but i cant figure out what some of my error messages are on my compiler. Also at the end i need to ask the user whether or not he wants to restart to program by just typing y to restart or any other button to exit.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int x, y, z;
  4.   char operator;
  5.  
  6.   cout<< " Welcome to the Calculator.\n";
  7.   cout<< " Input first number.\n";
  8.   cin>> x;
  9.   cout<< " Input + for addition, - for subtraction, * for multiplication, or / for division.\n";
  10.   cin>> operator;
  11.   cout<< " Input second number.\n";
  12.   cin>> y;
  13.  
  14.  
  15.   if (operator == "+")
  16.     { 
  17.      x+y=z;   
  18.      cout<<"The Answer is: "<<x+y=z<<".\n";
  19.     }
  20.  
  21.   if (operator == "-")
  22.     {
  23.      x-y=z;
  24.      cout<<"The Answer is: "<<x-y=z<<".\n";
  25.     }
  26.  
  27.   if (operator == "*")
  28.     {
  29.      x*y=z:
  30.      cout<<"The Answer is: "<<x*y=z<<".\n";
  31.     }
  32.  
  33.   if (operator == "/")
  34.     {
  35.       x/y=z;
  36.       cout<<"The Answer is: "<<(x/y)=z<<".\n";  
  37.       cout<<"The Remainder is: "<<(x%y)<<".\n";
  38.     }
  39.  
  40.   else
  41.     cout<<"Invalid Operator";
  42.  
  43.   return 0;
  44. }
Sep 20 '07 #14

sicarie
Expert Mod 2.5K+
P: 4,677
Can you post your error message?

And you want for a section of your code to repeat/loop while the user keeps inputting 'y'?
Sep 20 '07 #15

sicarie
Expert Mod 2.5K+
P: 4,677
Okay, I'm getting a weird error with your declaration of char operator, but besides that, you need to research variables in C/C++. You have several times

x + y = z

This takes the value in z, stores it in y, and then adds x. But you never save it, so it doesn't so anything with that value. You want:

z = x + y

Then you will have that value in z, and can use it later.
Sep 20 '07 #16

P: 67
Can you post your error message?

And you want for a section of your code to repeat/loop while the user keeps inputting 'y'?


I want for my whole program to rerun after at the end, but let it ask the user if he wants to rerun it or not.

expected primary-expression before "char"
expected ';' before char
expected identifier before ';' token
expected ')' before string constant
could not convert 'std::operator==' to 'bool'
non-lvalue in assignment
invalid operands of types 'int' and 'const char[3]' to binary 'operator<<'



thats some of them but they basically repeat because of each section
Sep 20 '07 #17

P: 67
Okay, I'm getting a weird error with your declaration of char operator, but besides that, you need to research variables in C/C++. You have several times

x + y = z

This takes the value in z, stores it in y, and then adds x. But you never save it, so it doesn't so anything with that value. You want:

z = x + y

Then you will have that value in z, and can use it later.

ya i did that last program i tried to write too.. you think i would have learned
Sep 20 '07 #18

P: 67
I want for my whole program to rerun after at the end, but let it ask the user if he wants to rerun it or not.

expected primary-expression before "char"
expected ';' before char
expected identifier before ';' token
expected ')' before string constant
could not convert 'std::operator==' to 'bool'
non-lvalue in assignment
invalid operands of types 'int' and 'const char[3]' to binary 'operator<<'



thats some of them but they basically repeat because of each section

Any ideas on what i should do because i need to try to get it done for tomorrow?
Sep 21 '07 #19

P: 67
My code is trying to ask for integer,operator, and another integer... and then use it to form an equation and print it out showing the exact equation... any ideas?
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int x, y, z;
  4.   char operator;
  5.  
  6.   cout<< " Welcome to the Calculator.\n";
  7.   cout<< " Input first number.\n";
  8.   cin>> x;
  9.   cout<< " Input + for addition, - for subtraction, * for multiplication, or / for division.\n";
  10.   cin>> operator;
  11.   cout<< " Input second number.\n";
  12.   cin>> y;
  13.  
  14.  
  15.   if (operator=+)
  16.     { 
  17.      z=x+y;   
  18.      cout<<"The Answer is: "<<x+y=z<<".\n";
  19.     }
  20.  
  21.   if (operator=-)
  22.     {
  23.      z=x-y;
  24.      cout<<"The Answer is: "<<x-y=z<<".\n";
  25.     }
  26.  
  27.   if (operator=*)
  28.     {
  29.      z=x*y:
  30.      cout<<"The Answer is: "<<x*y=z<<".\n";
  31.     }
  32.  
  33.   if (operator=/)
  34.     {
  35.       z=x/y;
  36.       cout<<"The Answer is: "<<(z=x/y)<<".\n";  
  37.       cout<<"The Remainder is: "<<(x%y)<<".\n";
  38.     }
  39.  
  40.   else
  41.     cout<<"Invalid Operator";
  42.  
  43.   return 0;
  44. }
Sep 21 '07 #20

ilikepython
Expert 100+
P: 844
My code is trying to ask for integer,operator, and another integer... and then use it to form an equation and print it out showing the exact equation... any ideas?

int main()
{
int x, y, z;
char operator;

cout<< " Welcome to the Calculator.\n";
cout<< " Input first number.\n";
cin>> x;
cout<< " Input + for addition, - for subtraction, * for multiplication, or / for division.\n";
cin>> operator;
cout<< " Input second number.\n";
cin>> y;


if (operator=+)
{
z=x+y;
cout<<"The Answer is: "<<x+y=z<<".\n";
}

if (operator=-)
{
z=x-y;
cout<<"The Answer is: "<<x-y=z<<".\n";
}

if (operator=*)
{
z=x*y:
cout<<"The Answer is: "<<x*y=z<<".\n";
}

if (operator=/)
{
z=x/y;
cout<<"The Answer is: "<<(z=x/y)<<".\n";
cout<<"The Remainder is: "<<(x%y)<<".\n";
}

else
cout<<"Invalid Operator";

return 0;
}
Your if statements are wrong:
Expand|Select|Wrap|Line Numbers
  1. if (operator == "+")
  2. {}
  3. else if (operator== "-")
  4. {}
  5. // ... so on ...
Also, you might have to delete the new line in the stream left by cin.
Sep 21 '07 #21

P: 67
Your if statements are wrong:
Expand|Select|Wrap|Line Numbers
  1. if (operator == "+")
  2. {}
  3. else if (operator== "-")
  4. {}
  5. // ... so on ...
Also, you might have to delete the new line in the stream left by cin.

What did you mean by that last part... You are probably right with the first part but that didnt change any of the error messages im getting with my code.. It actually gave me totally different errors..
Sep 21 '07 #22

ilikepython
Expert 100+
P: 844
What did you mean by that last part... You are probably right with the first part but that didnt change any of the error messages im getting with my code.. It actually gave me totally different errors..
Also, when you display the answer you do this:
Expand|Select|Wrap|Line Numbers
  1. z = x + y;
  2. cout << "The answer is: " << z << endl;
  3.  
  4. // ... or ...
  5. cout << "The answer is: " << x + y << endl; // no need for z variable
  6.  
Sep 21 '07 #23

P: 67
Also, when you display the answer you do this:
Expand|Select|Wrap|Line Numbers
  1. z = x + y;
  2. cout << "The answer is: " << z << endl;
  3.  
  4. // ... or ...
  5. cout << "The answer is: " << x + y << endl; // no need for z variable
  6.  

Well i was trying to make it so it would display my equation... so if x =5 and y= 6 it was print out 5+6=11
Sep 21 '07 #24

ilikepython
Expert 100+
P: 844
Well i was trying to make it so it would display my equation... so if x =5 and y= 6 it was print out 5+6=11
Well ok. You have the two numbers in the variables and you know how to print with cout so I think you could figure this out yourself. Remember you need to use quotes unless you are printing a variable.
Sep 21 '07 #25

P: 67
Well ok. You have the two numbers in the variables and you know how to print with cout so I think you could figure this out yourself. Remember you need to use quotes unless you are printing a variable.

I thought about what i should do so i put quotes around the +, - and such and i got a big error message... was that wrong?
Sep 21 '07 #26

P: 67
I thought about what i should do so i put quotes around the +, - and such and i got a big error message... was that wrong?

I think i figured that part out.... i have one error for every if statement line left...

it says expected primary expression before char and operator== not defined and expected ) before string constant
Sep 21 '07 #27

ilikepython
Expert 100+
P: 844
I thought about what i should do so i put quotes around the +, - and such and i got a big error message... was that wrong?
Did you do this:
Expand|Select|Wrap|Line Numbers
  1. cout << "The answer is: " << x "-" y "=" z << endl;
  2.  
You need to sperate them:
Expand|Select|Wrap|Line Numbers
  1. cout << "The asnwer is: " << x << "-" << y << "=" << z << endl;
  2.  
Sep 21 '07 #28

P: 67
Did you do this:
Expand|Select|Wrap|Line Numbers
  1. cout << "The answer is: " << x "-" y "=" z << endl;
  2.  
You need to sperate them:
Expand|Select|Wrap|Line Numbers
  1. cout << "The asnwer is: " << x << "-" << y << "=" << z << endl;
  2.  

it says expected identifier before ")" token for this part...

else if ((operator) = "/")
Sep 21 '07 #29

P: 67
int main()
{
int x, y, z;
char operator;

cout<< " Welcome to the Calculator.\n";
cout<< " Input first number.\n";
cin>> x;
cout<< " Input + for addition, - for subtraction, * for multiplication, or / for division.\n";
cin>> operator;
cout<< " Input second number.\n";
cin>> y;


if ((operator) = "+")
{
z=x+y;
cout<<"The Answer is: "<<x<<"+"<<y<<"="<<z<<".\n";
}

else if ((operator) = "-")
{
z=x-y;
cout<<"The Answer is: "<<x<<"-"<<y<<"="<<z<<".\n";
}

else if ((operator) = "*")
{
z=x*y;
cout<<"The Answer is: "<<x<<"*"<<y<<"="<<z<<".\n";
}

else if ((operator) = "/")
{
z=x/y;
cout<<"The Answer is: "<<x<<"/"<<y<<"="<<z<<".\n";
cout<<"The Remainder is: "<<x<<"%"<<y<<".\n";
}

else
(cout<<"Invalid Operator");

return 0;
}


I'm just stuck and don't know what to do... plus i need to make it rerun the program at the end by asking the user whether or not he wants to rerun the program.... but i have no idea...
Sep 21 '07 #30

100+
P: 121
Is this your code or where did you get it from?

I used the gcc compiler to test it and found:

Expand|Select|Wrap|Line Numbers
  1. charlie$ gcc test.c
  2. test.c: In function `main':
  3. test.c:6: error: `cout' undeclared (first use in this function)
  4. test.c:6: error: (Each undeclared identifier is reported only once
  5. test.c:6: error: for each function it appears in.)
  6. test.c:8: error: `cin' undeclared (first use in this function)
  7. test.c:15: warning: assignment makes integer from pointer without a cast
  8. test.c:21: warning: assignment makes integer from pointer without a cast
  9. test.c:27: warning: assignment makes integer from pointer without a cast
  10. test.c:33: warning: assignment makes integer from pointer without a cast
  11. test.c:44:2: warning: no newline at end of file
  12.  
Something is missing in your code.
Sep 21 '07 #31

P: 67
Is this your code or where did you get it from?

I used the gcc compiler to test it and found:

Expand|Select|Wrap|Line Numbers
  1. charlie$ gcc test.c
  2. test.c: In function `main':
  3. test.c:6: error: `cout' undeclared (first use in this function)
  4. test.c:6: error: (Each undeclared identifier is reported only once
  5. test.c:6: error: for each function it appears in.)
  6. test.c:8: error: `cin' undeclared (first use in this function)
  7. test.c:15: warning: assignment makes integer from pointer without a cast
  8. test.c:21: warning: assignment makes integer from pointer without a cast
  9. test.c:27: warning: assignment makes integer from pointer without a cast
  10. test.c:33: warning: assignment makes integer from pointer without a cast
  11. test.c:44:2: warning: no newline at end of file
  12.  
Something is missing in your code.
I know im missing something i just cant figure out what??
Sep 21 '07 #32

P: 92
Two things.

1) Put an extra newline at the end of your code. gcc annoyingly reports this although its not really a problem.

2) You need some includes here. cout is not part of the C++ language persay but rather part of the iostream package. So just put this at the top of your file:

#include <iostream>
Sep 21 '07 #33

P: 67
I really need some help to figure this out which is due tomorrow... so anyone that can help please send something
Sep 21 '07 #34

P: 67
Two things.

1) Put an extra newline at the end of your code. gcc annoyingly reports this although its not really a problem.

2) You need some includes here. cout is not part of the C++ language persay but rather part of the iostream package. So just put this at the top of your file:

#include <iostream>

i do have that at the top of my code i just didnt paste it in.... what line do i need to add to the bottom
Sep 21 '07 #35

P: 92
The bottom is just hit enter one more time.

As for the other issues you may need:

using namespace std;

before the main function
Sep 21 '07 #36

P: 67
The bottom is just hit enter one more time.

As for the other issues you may need:

using namespace std;

before the main function
i have that too... let me give u my errors... im using emacs....

expected primary-expression before "char"
expected ';' before "char"
expected identifier before ';' token
expected identifier before ')' token.... and the continues for three more of them
Sep 21 '07 #37

P: 67
Anyone else with any ideas... i need help!!!
Sep 21 '07 #38

P: 92
Oh ok, confused by the other list of errors. Three issues I found when compiling this:

1. The symbol 'operator' is reserved and cannot be used as a variable name. So this trips up the code. Just rename it and all the places you use it.

2. The operator '=' you use in your if statements are assignment operators not the comparison operator, which is '=='. So the code you have is trying to assign a new value to the variable.

3. In your if statments you are comparing a char to a string. The "+" means a string but since your variable is a single character you want '+'

Fix these up and the code should compile for you.
Sep 21 '07 #39

P: 67
Oh ok, confused by the other list of errors. Three issues I found when compiling this:

1. The symbol 'operator' is reserved and cannot be used as a variable name. So this trips up the code. Just rename it and all the places you use it.

2. The operator '=' you use in your if statements are assignment operators not the comparison operator, which is '=='. So the code you have is trying to assign a new value to the variable.

3. In your if statments you are comparing a char to a string. The "+" means a string but since your variable is a single character you want '+'

Fix these up and the code should compile for you.
the last thing i have to ask is that if i want to ask the user whether or not he wants to rerun the program... how do i do that... i just want to have him have to enter like y to run again or any other key to exit...
Sep 21 '07 #40

P: 92
You can't rerun the program per day. What you want is to do a loop in your program to rerun the code you have. An example of this would be:

char rerun_choice = 'c';

Expand|Select|Wrap|Line Numbers
  1. while (rerun_choice == 'y')
  2.   {
  3.   cout << "continue? ";
  4.   cin >> rerun_choice;
  5.   }
In this code as long as the user hits y <enter> they will be asked again and again. Any other letter and it exits the loop. Actually the better approach here would be a do while loop. Similar to above but the while part is at the bottom, so it runs once regardless of the check. Lookup in your C manual on how to use, its a nice C feature.
Sep 21 '07 #41

P: 67
You can't rerun the program per day. What you want is to do a loop in your program to rerun the code you have. An example of this would be:

char rerun_choice = 'c';

Expand|Select|Wrap|Line Numbers
  1. while (rerun_choice == 'y')
  2.   {
  3.   cout << "continue? ";
  4.   cin >> rerun_choice;
  5.   }
In this code as long as the user hits y <enter> they will be asked again and again. Any other letter and it exits the loop. Actually the better approach here would be a do while loop. Similar to above but the while part is at the bottom, so it runs once regardless of the check. Lookup in your C manual on how to use, its a nice C feature.
the only thing that this did was ask if i wanted to continue... it didnt rerun the whole program... also everytime i try anything other than addition it still adds...
Sep 21 '07 #42

P: 92
You need to put you code in this while loop. Anything in the while loop will be executed. So put the while loop around the code you already had.
Sep 21 '07 #43

P: 67
You need to put you code in this while loop. Anything in the while loop will be executed. So put the while loop around the code you already had.
i put it in and its exited my program no matter what i put in.
Sep 21 '07 #44

P: 92
Post your current code again. I must have missed something.
Sep 21 '07 #45

P: 67
#include <iostream>
using namespace std;

int main()
{
int x, y, z;
char oper, rerun_choice = 'c';

while (rerun_choice == 'y');
{

cout<< " Welcome to the Calculator.\n";
cout<< " Input first number.\n";
cin>> x;
cout<< " Input + for addition, - for subtraction, * for multiplication, or / for division.\n";
cin>> oper;
cout<< " Input second number.\n";
cin>> y;


if ((oper) = '+')
{
z=x+y;
cout<<"The Answer is: "<<x<<"+"<<y<<"="<<z<<".\n";
}

if ((oper) = '-')
{
z=x-y;
cout<<"The Answer is: "<<x<<"-"<<y<<"="<<z<<".\n";
}

if ((oper) = '*')
{
z=x*y;
cout<<"The Answer is: "<<x<<"*"<<y<<"="<<z<<".\n";
}

if ((oper) = '/')
{
z=x/y;
cout<<"The Answer is: "<<x<<"/"<<y<<"="<<z<<".\n";
cout<<"The Remainder is: "<<x<<"%"<<y<<".\n";
}

cout<<"continue?";
cin>> rerun_choice;
}

return 0;

}




I have a couple questions....

1) why the rerun isnt working
2) when i run the program it is printing out all the equations for +,-,*,/ and also for % which i want to be the remainder
2b) the remainder is not coming out to be what i want.. instead its just plugging in the normal values for x and y and shoving % in the middle...
Sep 21 '07 #46

P: 92
1) why the rerun isnt working
Ok the code I gave should have set the rerun_choice to 'y' initially. Whats happening is its failing the while loop from the onset. Just use a do...while loop. Easy to change just move the while () line to the end of the loop (ie just before the return call) and where the while was put a do. Then it does not matter what you set the initial value of the rerun_choice to since the evaluation happens at the end.

so it would be

Expand|Select|Wrap|Line Numbers
  1. do {
  2. <all your code>
  3. } while (rerun_choice == 'y');
  4.  

2) when i run the program it is printing out all the equations for +,-,*,/ and also for % which i want to be the remainder
2b) the remainder is not coming out to be what i want.. instead its just plugging in the normal values for x and y and shoving % in the middle...
That's because the cout line just output data and you have the % in there as a string not an operator, they do not calculate anything. So rework the line to be:

Expand|Select|Wrap|Line Numbers
  1. cout<<"The Remainder is: "<<(x % y)<<".\n";
Sep 21 '07 #47

P: 67
The remainder part is fixed and i put in the do while but it is still just exiting the program... also i don't want it to print out all of the equations i only want it to print the + equation if i choose + and the - if i choose -...
Sep 21 '07 #48

P: 92
When asked to continue are you hitting 'y' and then enter?

As for the part about it executing all of the operations, it's your if statements. Two items I would do:
1) You are still not using the comparator. All comparison should use '==' not the single = value.

2) I would also do if, else if. So we would have:

Expand|Select|Wrap|Line Numbers
  1. if (oper == '+')
  2.   {
  3.   z=x+y;
  4.   cout<<"The Answer is: "<<x<<"+"<<y<<"="<<z<<".\n";
  5.   }
  6.  
  7. else if (oper == '-')
  8.   {
  9.   z=x-y;
  10.   cout<<"The Answer is: "<<x<<"-"<<y<<"="<<z<<".\n";
  11.   }
  12.  
  13. ....
  14.  
Finally, when posting code, try to use the code block (the # button when posting) it makes things a little easier to read.
Sep 21 '07 #49

P: 67
When asked to continue are you hitting 'y' and then enter?

As for the part about it executing all of the operations, it's your if statements. Two items I would do:
1) You are still not using the comparator. All comparison should use '==' not the single = value.

2) I would also do if, else if. So we would have:

Expand|Select|Wrap|Line Numbers
  1. if (oper == '+')
  2.   {
  3.   z=x+y;
  4.   cout<<"The Answer is: "<<x<<"+"<<y<<"="<<z<<".\n";
  5.   }
  6.  
  7. else if (oper == '-')
  8.   {
  9.   z=x-y;
  10.   cout<<"The Answer is: "<<x<<"-"<<y<<"="<<z<<".\n";
  11.   }
  12.  
  13. ....
  14.  
Finally, when posting code, try to use the code block (the # button when posting) it makes things a little easier to read.
instead of asking to continue its just rerunning the program... im not sure if i have everything placed right...
Sep 21 '07 #50

63 Replies

Post your reply

Sign in to post your reply or Sign up for a free account.