Equation printing program

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

    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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

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

    Comment

    • socondc22
      New Member
      • Sep 2007
      • 67

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



      Originally posted by JosAH
      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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

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

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

        Comment

        • socondc22
          New Member
          • Sep 2007
          • 67

          #5
          I don't quite understand how that works.

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

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

          Comment

          • socondc22
            New Member
            • Sep 2007
            • 67

            #6
            Anyone else have a simpler idea or can explain it for me.

            Comment

            • kreagan
              New Member
              • Aug 2007
              • 153

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

              Comment

              • socondc22
                New Member
                • Sep 2007
                • 67

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


                [CODE=cpp]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=answe r;
                cout<< " The Answer is: "<<num1+num2=an swer<<".\n";
                break;

                case 2:

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

                case 3:

                num1*num2=answe r;
                cout<< " The Answer is: "<<num1*num2=an swer<<".\n";
                break;

                case 4:

                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;
                }[/CODE]
                Last edited by Ganon11; Sep 20 '07, 03:26 AM. Reason: Please use the [CODE] tags provided.

                Comment

                • kreagan
                  New Member
                  • Aug 2007
                  • 153

                  #9
                  Originally posted by socondc22
                  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=answe r;
                  cout<< " The Answer is: "<<num1+num2=an swer<<".\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).

                  Comment

                  • socondc22
                    New Member
                    • Sep 2007
                    • 67

                    #10
                    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?

                    Originally posted by kreagan
                    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).

                    Comment

                    • kreagan
                      New Member
                      • Aug 2007
                      • 153

                      #11
                      Originally posted by socondc22
                      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 ;)

                      Comment

                      • socondc22
                        New Member
                        • Sep 2007
                        • 67

                        #12
                        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???

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          Originally posted by kreagan
                          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?

                          Comment

                          • socondc22
                            New Member
                            • Sep 2007
                            • 67

                            #14
                            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.
                            [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 == "+")
                            {
                            x+y=z;
                            cout<<"The Answer is: "<<x+y=z<<".\n" ;
                            }

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

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

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

                            else
                            cout<<"Invalid Operator";

                            return 0;
                            }[/code]
                            Last edited by sicarie; Sep 20 '07, 08:17 PM. Reason: Code tags are to the right when creating a post, please use them.

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #15
                              Can you post your error message?

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

                              Comment

                              Working...