Equation printing program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #16
    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.

    Comment

    • socondc22
      New Member
      • Sep 2007
      • 67

      #17
      Originally posted by sicarie
      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

      Comment

      • socondc22
        New Member
        • Sep 2007
        • 67

        #18
        Originally posted by sicarie
        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

        Comment

        • socondc22
          New Member
          • Sep 2007
          • 67

          #19
          Originally posted by socondc22
          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?

          Comment

          • socondc22
            New Member
            • Sep 2007
            • 67

            #20
            Equation printing program

            My code is trying to ask for integer,operato r, and another integer... and then use it to form an equation and print it out showing the exact equation... any ideas?
            [code=cpp]
            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;
            }[/code]
            Last edited by sicarie; Sep 21 '07, 12:43 PM. Reason: Code tags are to the right when creating a post, please use them.

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #21
              Originally posted by socondc22
              My code is trying to ask for integer,operato r, 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:
              [code=cpp]
              if (operator == "+")
              {}
              else if (operator== "-")
              {}
              // ... so on ...[/code]
              Also, you might have to delete the new line in the stream left by cin.

              Comment

              • socondc22
                New Member
                • Sep 2007
                • 67

                #22
                Originally posted by ilikepython
                Your if statements are wrong:
                [code=cpp]
                if (operator == "+")
                {}
                else if (operator== "-")
                {}
                // ... so on ...[/code]
                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..

                Comment

                • ilikepython
                  Recognized Expert Contributor
                  • Feb 2007
                  • 844

                  #23
                  Originally posted by socondc22
                  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:
                  [code=cpp]
                  z = x + y;
                  cout << "The answer is: " << z << endl;

                  // ... or ...
                  cout << "The answer is: " << x + y << endl; // no need for z variable
                  [/code]

                  Comment

                  • socondc22
                    New Member
                    • Sep 2007
                    • 67

                    #24
                    Originally posted by ilikepython
                    Also, when you display the answer you do this:
                    [code=cpp]
                    z = x + y;
                    cout << "The answer is: " << z << endl;

                    // ... or ...
                    cout << "The answer is: " << x + y << endl; // no need for z variable
                    [/code]

                    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

                    Comment

                    • ilikepython
                      Recognized Expert Contributor
                      • Feb 2007
                      • 844

                      #25
                      Originally posted by socondc22
                      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.

                      Comment

                      • socondc22
                        New Member
                        • Sep 2007
                        • 67

                        #26
                        Originally posted by ilikepython
                        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?

                        Comment

                        • socondc22
                          New Member
                          • Sep 2007
                          • 67

                          #27
                          Originally posted by socondc22
                          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

                          Comment

                          • ilikepython
                            Recognized Expert Contributor
                            • Feb 2007
                            • 844

                            #28
                            Originally posted by socondc22
                            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:
                            [code=cpp]
                            cout << "The answer is: " << x "-" y "=" z << endl;
                            [/code]
                            You need to sperate them:
                            [code=cpp]
                            cout << "The asnwer is: " << x << "-" << y << "=" << z << endl;
                            [/code]

                            Comment

                            • socondc22
                              New Member
                              • Sep 2007
                              • 67

                              #29
                              Originally posted by ilikepython
                              Did you do this:
                              [code=cpp]
                              cout << "The answer is: " << x "-" y "=" z << endl;
                              [/code]
                              You need to sperate them:
                              [code=cpp]
                              cout << "The asnwer is: " << x << "-" << y << "=" << z << endl;
                              [/code]

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

                              else if ((operator) = "/")

                              Comment

                              • socondc22
                                New Member
                                • Sep 2007
                                • 67

                                #30
                                a couple more erros left in my code??

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

                                Comment

                                Working...