473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Equation printing program

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
63 5305
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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
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
Anyone else have a simpler idea or can explain it for me.
Sep 19 '07 #6
kreagan
153 100+
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
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
153 100+
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
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
153 100+
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
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
4,677 Expert Mod 4TB
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
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
4,677 Expert Mod 4TB
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
4,677 Expert Mod 4TB
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
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
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
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
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
844 Expert 512MB
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
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
844 Expert 512MB
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
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
844 Expert 512MB
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
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
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
844 Expert 512MB
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
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
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
mattmao
121 100+
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
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
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
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
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
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
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
Anyone else with any ideas... i need help!!!
Sep 21 '07 #38
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
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
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
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
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
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
Post your current code again. I must have missed something.
Sep 21 '07 #45
#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
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
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
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
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

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

Similar topics

1
by: Russell Blau | last post by:
This is not really a Python question, but it does relate to a Python program I'm working on, so this seems like as good a place as any to ask for suggestions ... I'm working on a GUI application...
20
by: Brian Kazian | last post by:
Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using...
2
by: Michael MacDonald | last post by:
I have written a simple program that calculates a running $ total. This program uses a checkbox to acknowledge that that version of the ticket is desired and it checks the Qty box to see how many...
6
by: Trev17 | last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a...
11
by: shanazzle | last post by:
so I've just begun learning python for a school course. I have to write a simple program that finds the first derivative of the equation Y= Ax^4 + Bx^3 + Cx^2 + Dx + E so far i can have the user...
1
by: kjcheste | last post by:
I'm trying to have C++ solve an equation I have set up. The equation is: angle = (1/16)*(pow(LS,2) + LS*sqrt(pow(LS,2) + pow(16,2) - 1) - 1); where LS is a number from -1 to 1 by increasing it...
2
by: ioannoual | last post by:
Hi...I 'am new in C and I want a program that solves a quadratic equation!! I try something by my self to write some code about that but I want you to help me writing a new one that will work!! ...
1
by: bbench123 | last post by:
Make a program that will ask for values of a quadratic equation (ax2+bx+c). the program must determine the roots of the equation using the quadratic equation determinants to distinguish if the roots...
1
by: candacefaye1 | last post by:
1. write a C++ program to decide if the coefficients of a quadratic equation have real roots. The three choices will be to write the message “zero divide” when A is zero, write the message “no real...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.