449,315 Members | 1,661 Online
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
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 int a= askInteger("please give left operand"); char op= askCharacter("please give one of +-*/"); int b= askInteger("please give right operand");   int result; switch (op) {    // according to the value of op, assign the     // correct value to variable 'result' } printf("%d%c%d=%d\n", a, op, b, result);   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 int a= askInteger("please give left operand"); char op= askCharacter("please give one of +-*/"); int b= askInteger("please give right operand");   int result; switch (op) {    // according to the value of op, assign the     // correct value to variable 'result' } printf("%d%c%d=%d\n", a, op, b, result);   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

 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 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: "<

 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: "<

 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

 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

 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 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 == "+")     {       x+y=z;         cout<<"The Answer is: "<

 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

 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 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: "<

 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: "<

 P: 67 Your if statements are wrong: Expand|Select|Wrap|Line Numbers if (operator == "+") {} else if (operator== "-") {} // ... 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

 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 z = x + y; cout << "The answer is: " << z << endl;   // ... or ... cout << "The answer is: " << x + y << endl; // no need for z variable   Sep 21 '07 #23

 P: 67 Also, when you display the answer you do this: Expand|Select|Wrap|Line Numbers z = x + y; cout << "The answer is: " << z << endl;   // ... or ... cout << "The answer is: " << x + y << endl; // no need for z variable   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

 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

 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 cout << "The answer is: " << x "-" y "=" z << endl;   You need to sperate them: Expand|Select|Wrap|Line Numbers cout << "The asnwer is: " << x << "-" << y << "=" << z << endl;   Sep 21 '07 #28

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

 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 charlie\$ gcc test.c test.c: In function `main': test.c:6: error: `cout' undeclared (first use in this function) test.c:6: error: (Each undeclared identifier is reported only once test.c:6: error: for each function it appears in.) test.c:8: error: `cin' undeclared (first use in this function) test.c:15: warning: assignment makes integer from pointer without a cast test.c:21: warning: assignment makes integer from pointer without a cast test.c:27: warning: assignment makes integer from pointer without a cast test.c:33: warning: assignment makes integer from pointer without a cast test.c:44:2: warning: no newline at end of file   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 charlie\$ gcc test.c test.c: In function `main': test.c:6: error: `cout' undeclared (first use in this function) test.c:6: error: (Each undeclared identifier is reported only once test.c:6: error: for each function it appears in.) test.c:8: error: `cin' undeclared (first use in this function) test.c:15: warning: assignment makes integer from pointer without a cast test.c:21: warning: assignment makes integer from pointer without a cast test.c:27: warning: assignment makes integer from pointer without a cast test.c:33: warning: assignment makes integer from pointer without a cast test.c:44:2: warning: no newline at end of file   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 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 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 while (rerun_choice == 'y')   {   cout << "continue? ";   cin >> rerun_choice;   } In this code as long as the user hits y 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 while (rerun_choice == 'y')   {   cout << "continue? ";   cin >> rerun_choice;   } In this code as long as the user hits y 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 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: "<> 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 do { } while (rerun_choice == 'y');   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 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 if (oper == '+')   {   z=x+y;   cout<<"The Answer is: "<

 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 if (oper == '+')   {   z=x+y;   cout<<"The Answer is: "<

63 Replies