Equation printing program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • socondc22
    New Member
    • Sep 2007
    • 67

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

    Comment

    • Benny the Guard
      New Member
      • Jun 2007
      • 92

      #47
      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

      Code:
      do {
      <all your code>
      } 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:

      Code:
      cout<<"The Remainder is: "<<(x % y)<<".\n";

      Comment

      • socondc22
        New Member
        • Sep 2007
        • 67

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

        Comment

        • Benny the Guard
          New Member
          • Jun 2007
          • 92

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

          Code:
          if (oper == '+')
            {
            z=x+y;
            cout<<"The Answer is: "<<x<<"+"<<y<<"="<<z<<".\n";
            }
          
          else if (oper == '-')
            {
            z=x-y;
            cout<<"The Answer is: "<<x<<"-"<<y<<"="<<z<<".\n";
            }
          
          ....
          Finally, when posting code, try to use the code block (the # button when posting) it makes things a little easier to read.

          Comment

          • socondc22
            New Member
            • Sep 2007
            • 67

            #50
            Originally posted by Benny the Guard
            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:

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

            Comment

            • socondc22
              New Member
              • Sep 2007
              • 67

              #51
              looping to rerun program

              Im using a do while loop to try to rerun my program at the end so that itll ask me to type y in order to rerun... everytime i type y and hit enter it exits the program... any ideas??

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #52
                Originally posted by socondc22
                Im using a do while loop to try to rerun my program at the end so that itll ask me to type y in order to rerun... everytime i type y and hit enter it exits the program... any ideas??
                Can you show us how have you implemented this do while loop?

                Savage

                Comment

                • socondc22
                  New Member
                  • Sep 2007
                  • 67

                  #53
                  Originally posted by Savage
                  Can you show us how have you implemented this do while loop?

                  Savage

                  i used

                  char rerun_choice = 'c';

                  Code:
                  do
                              {
                                MY CODE
                              }
                                 while (rerun_choice == 'y');
                                 cout<<"run again type y"<<".\n";
                                 cin>> rerun_choice;

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #54
                    Originally posted by socondc22
                    i used

                    char rerun_choice = 'c';

                    [CODE="cpp"]do
                    {
                    MY CODE
                    }
                    while (rerun_choice == 'y');
                    cout<<"run again type y"<<".\n";
                    cin>> rerun_choice;

                    [/CODE]
                    Shouldn't that be inside the loop?

                    [CODE="cpp"]do{
                    YOUR CODE

                    cout<<"To run again type y"<<endl;
                    cin>> rerun_choice;

                    }while(rerun_ch oice=='Y')[/CODE]

                    Also you should convert user input to upper letters(what if user inputed y?)
                    You can use toupper(),defin ed in ctype.h

                    Savage

                    Comment

                    • ilikepython
                      Recognized Expert Contributor
                      • Feb 2007
                      • 844

                      #55
                      Originally posted by socondc22
                      it says expected identifier before ")" token for this part...

                      else if ((operator) = "/")
                      You shouldn't need the second set of parenthesis and you need the double equal sign ('==')

                      [code=cpp]
                      if (operator == "+"){}
                      else if (operator == "/"){}
                      [/code]

                      Comment

                      • archonmagnus
                        New Member
                        • Jun 2007
                        • 113

                        #56
                        Your variable, "operator", is of type char. This means you might need to use a single quote rather than double quotes as follows:

                        [code=cpp]
                        if (operator == '+')
                        cout<<"The Answer is: "<< x << "+" << y << "=" << x + y <<endl;


                        else if (operator == '-')
                        cout<<"The Answer is: "<< x << "-" << y << "=" << x - y <<endl;


                        else if (operator = '*')
                        cout<<"The Answer is: "<< x << "*" << y << "=" << x * y <<endl;

                        else if (operator = '/')
                        {
                        cout<<"The Answer is: "<< x << "/" << y << "=" << x / y <<endl;
                        cout<<"The Remainder is: "<< x << "%" << y << "=" << x % y <<endl;
                        }

                        else
                        cout<<"Bad operator input, try again..."<<endl ;
                        [/code]

                        Comment

                        • ilikepython
                          Recognized Expert Contributor
                          • Feb 2007
                          • 844

                          #57
                          Originally posted by archonmagnus
                          Your variable, "operator", is of type char. This means you might need to use a single quote rather than double quotes as follows:

                          [code=cpp]
                          if (operator == '+')
                          cout<<"The Answer is: "<< x << "+" << y << "=" << x + y <<endl;


                          else if (operator == '-')
                          cout<<"The Answer is: "<< x << "-" << y << "=" << x - y <<endl;


                          else if (operator = '*')
                          cout<<"The Answer is: "<< x << "*" << y << "=" << x * y <<endl;

                          else if (operator = '/')
                          {
                          cout<<"The Answer is: "<< x << "/" << y << "=" << x / y <<endl;
                          cout<<"The Remainder is: "<< x << "%" << y << "=" << x % y <<endl;
                          }

                          else
                          cout<<"Bad operator input, try again..."<<endl ;
                          [/code]
                          Yep, I missed that, sorry.

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #58
                            Originally posted by socondc22
                            Any ideas on what i should do because i need to try to get it done for tomorrow?
                            After you made the assignment, what error messages did you get?

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #59
                              Originally posted by sicarie
                              After you made the assignment, what error messages did you get?

                              And this is an example of why we ask people to not double post.

                              Socondc22 - please read our Posting Guidelines and follow them in the future (not double-posting). Thanks.

                              Comment

                              • sicarie
                                Recognized Expert Specialist
                                • Nov 2006
                                • 4677

                                #60
                                Originally posted by socondc22
                                instead of asking to continue its just rerunning the program... im not sure if i have everything placed right...
                                And this is another example of why you shouldn't double (triple) post. Did you take Savage's advice in your other thread and put the prompt inside the do-while loop?

                                Comment

                                Working...