Gonzo, help me please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 05l8kr
    New Member
    • Sep 2006
    • 35

    Gonzo, help me please

    I'm suppose to write a program that will ask the user to enter two integers. Then the program will ask the user for one of four letters a to add the numbers, s to subtract the numbers, m to multiply the numbers, or d to divide the two numbers.
    If the user enters any character other than the 4 listed, keep asking them over and over until they enter a correct letter. Display on the monitor the two numbers entered the math operation used and the answer of the operation:

    What's your aim name? PM it
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Is there supposed to be any error-checking for the numbers? Or just for the letters?

    All you need to do is cin two numbers, then enter a while loop. The while loop will have a condition that a char i == 'a' or i == 's' or i == 'm' or i == 'd'. While you are in that loop, cin i.

    Then, you can either use if statements or a switch statement to do the correct operation.

    Comment

    • 05l8kr
      New Member
      • Sep 2006
      • 35

      #3
      Originally posted by sicarie
      Is there supposed to be any error-checking for the numbers? Or just for the letters?

      All you need to do is cin two numbers, then enter a while loop. The while loop will have a condition that a char i == 'a' or i == 's' or i == 'm' or i == 'd'. While you are in that loop, cin i.

      Then, you can either use if statements or a switch statement to do the correct operation.
      Just the letters

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Well, then you got it.

        Comment

        • 05l8kr
          New Member
          • Sep 2006
          • 35

          #5
          Code:
          == 'a' or i == 's' or i == 'm' or i == 'd'
          That's all the code ???

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by karlracki
            Code:
            == 'a' or i == 's' or i == 'm' or i == 'd'
            That's all the code ???
            Absolutely not - I'm not writing the program, you are.

            That is the general pseudocode or algorithm to follow. If you break that down and code it, you'll get it. (Though not that little bit there, the whole paragraph was the pseudocode, you just zeroed in on what looked the most like code).

            Feel free to post any compiler errors you run into or any sort of declarational statements, but it wouldn't be a learning experience if you didn't have to do a little thinking ;)

            Comment

            • khajeddin
              New Member
              • Nov 2006
              • 51

              #7
              hi :im khajeddin.
              you should write this program with "switch" structure.and also make a loop that ask the user over and oner again to enter the correct character.here it is what you want:
              (then please answer my question which is in the Discussion page of C/C++)


              while(1)
              {
              switch(ch) {
              case'a' :
              result=number1+ numder2;
              break;
              case's' :
              result=number1-number2;
              break;
              case'm' :
              result=number1* number2;
              break;
              case'd' :
              result=number1/number2;
              break;
              default:
              printf("please enter the correct character");
              break;
              } /*end of while*/

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by khajeddin
                hi :im khajeddin.
                you should write this program with "switch" structure.and also make a loop that ask the user over and oner again to enter the correct character.here it is what you want:
                (then please answer my question which is in the Discussion page of C/C++)


                while(1)
                {
                switch(ch) {
                case'a' :
                result=number1+ numder2;
                break;
                case's' :
                result=number1-number2;
                break;
                case'm' :
                result=number1* number2;
                break;
                case'd' :
                result=number1/number2;
                break;
                default:
                printf("please enter the correct character");
                break;
                } /*end of while*/
                Man, i always forget about switch statements - good idea.

                Comment

                • 05l8kr
                  New Member
                  • Sep 2006
                  • 35

                  #9
                  Originally posted by sicarie
                  Man, i always forget about switch statements - good idea.
                  What should loop look like for the user over and oner again to enter the correct character

                  Comment

                  • sivadhas2006
                    New Member
                    • Nov 2006
                    • 142

                    #10
                    Hi,

                    This may satisfy your need.

                    Code:
                    int DoOperation(int a_nFirst, int a_nSecond, char &a_cOperation)
                    {
                       int
                          nResult = 0;
                    
                       switch(a_cOperation) 
                       {
                          case'a' :
                          {
                             nResult = a_nFirst + a_nSecond;
                             break;
                          }
                    
                          case's' :
                          {
                             nResult = a_nFirst - a_nSecond;
                             break;
                          }
                    
                          case'm' :
                          {
                             nResult = a_nFirst * a_nSecond;
                             break;
                          }
                          
                          case'd' :
                          {
                             nResult = a_nFirst / a_nSecond;
                             break;
                          }
                    
                          case'q' :
                          {         
                             // To quit the recursive.
                             break;
                          }
                          
                          default:
                          {
                             cout << "Please enter the correct operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
                             cin >> a_cOperation;
                             
                             // Recursive function to do the operation again.
                             nResult = DoOperation(a_nFirst, a_nSecond, a_cOperation);
                             break;
                          }      
                       }
                       return nResult;
                    }
                    
                    void main()
                    {
                       int      
                          nFirst = 0,      
                          nSecond = 0,
                          nResult = 0;
                       char
                          cOperation = '\0';
                    
                       // Get the first number.
                       cout << "Enter the first number : ";
                       cin >> nFirst;
                    
                       // Get the second number.
                       cout << "Enter the second number : ";
                       cin >> nSecond;
                    
                       // Get the operator.
                       cout << "Enter the operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
                       cin >> cOperation;
                    
                       // Do the operation with the given information.
                       nResult = DoOperation(nFirst, nSecond, cOperation);
                    
                       // Print the result.
                       cout << "The result is : " << nFirst << " " << cOperation << " " << nSecond << " = " << nResult;
                    
                    }
                    Regards,
                    M.Sivadhas.

                    Comment

                    • 05l8kr
                      New Member
                      • Sep 2006
                      • 35

                      #11
                      Code:
                      { while(1)
                      {
                      switch(ch) {
                      case'a' :
                      result=number1+numder2;
                      break;
                      case's' :
                      result=number1-number2;
                      break;
                      case'm' :
                      result=number1*number2;
                      break;
                      case'd' :
                      result=number1/number2;
                      break;
                      default:
                      printf("please enter the correct character");
                      break;
                      } /*end of while*/
                      int DoOperation(int a_nFirst, int a_nSecond, char &a_cOperation)
                      {
                         int
                            nResult = 0;
                      
                         switch(a_cOperation) 
                         {
                            case'a' :
                            {
                               nResult = a_nFirst + a_nSecond;
                               break;
                            }
                      
                            case's' :
                            {
                               nResult = a_nFirst - a_nSecond;
                               break;
                            }
                      
                            case'm' :
                            {
                               nResult = a_nFirst * a_nSecond;
                               break;
                            }
                            
                            case'd' :
                            {
                               nResult = a_nFirst / a_nSecond;
                               break;
                            }
                      
                            case'q' :
                            {         
                               // To quit the recursive.
                               break;
                            }
                            
                            default:
                            {
                               cout << "Please enter the correct operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
                               cin >> a_cOperation;
                               
                               // Recursive function to do the operation again.
                               nResult = DoOperation(a_nFirst, a_nSecond, a_cOperation);
                               break;
                            }      
                         }
                         return nResult;
                      }
                      
                      void main()
                      {
                         int      
                            nFirst = 0,      
                            nSecond = 0,
                            nResult = 0;
                         char
                            cOperation = '\0';
                      
                         // Get the first number.
                         cout << "Enter the first number : ";
                         cin >> nFirst;
                      
                         // Get the second number.
                         cout << "Enter the second number : ";
                         cin >> nSecond;
                      
                         // Get the operator.
                         cout << "Enter the operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
                         cin >> cOperation;
                      
                         // Do the operation with the given information.
                         nResult = DoOperation(nFirst, nSecond, cOperation);
                      
                         // Print the result.
                         cout << "The result is : " << nFirst << " " << cOperation << " " << nSecond << " = " << nResult;
                      
                      }
                      I got the following errors
                      -fatal error C1004: unexpected end of file found
                      Error executing cl.exe.
                      -error C2447: missing function header (old-style formal list?)

                      Comment

                      • sivadhas2006
                        New Member
                        • Nov 2006
                        • 142

                        #12
                        Originally posted by karlracki
                        Code:
                        { while(1)
                        {
                        switch(ch) {
                        case'a' :
                        result=number1+numder2;
                        break;
                        case's' :
                        result=number1-number2;
                        break;
                        case'm' :
                        result=number1*number2;
                        break;
                        case'd' :
                        result=number1/number2;
                        break;
                        default:
                        printf("please enter the correct character");
                        break;
                        } /*end of while*/
                        int DoOperation(int a_nFirst, int a_nSecond, char &a_cOperation)
                        {
                           int
                              nResult = 0;
                        
                           switch(a_cOperation) 
                           {
                              case'a' :
                              {
                                 nResult = a_nFirst + a_nSecond;
                                 break;
                              }
                        
                              case's' :
                              {
                                 nResult = a_nFirst - a_nSecond;
                                 break;
                              }
                        
                              case'm' :
                              {
                                 nResult = a_nFirst * a_nSecond;
                                 break;
                              }
                              
                              case'd' :
                              {
                                 nResult = a_nFirst / a_nSecond;
                                 break;
                              }
                        
                              case'q' :
                              {         
                                 // To quit the recursive.
                                 break;
                              }
                              
                              default:
                              {
                                 cout << "Please enter the correct operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
                                 cin >> a_cOperation;
                                 
                                 // Recursive function to do the operation again.
                                 nResult = DoOperation(a_nFirst, a_nSecond, a_cOperation);
                                 break;
                              }      
                           }
                           return nResult;
                        }
                        
                        void main()
                        {
                           int      
                              nFirst = 0,      
                              nSecond = 0,
                              nResult = 0;
                           char
                              cOperation = '\0';
                        
                           // Get the first number.
                           cout << "Enter the first number : ";
                           cin >> nFirst;
                        
                           // Get the second number.
                           cout << "Enter the second number : ";
                           cin >> nSecond;
                        
                           // Get the operator.
                           cout << "Enter the operator(a -> add, s -> sub, m -> mul, d -> div, q -> quit : ";
                           cin >> cOperation;
                        
                           // Do the operation with the given information.
                           nResult = DoOperation(nFirst, nSecond, cOperation);
                        
                           // Print the result.
                           cout << "The result is : " << nFirst << " " << cOperation << " " << nSecond << " = " << nResult;
                        
                        }
                        I got the following errors
                        -fatal error C1004: unexpected end of file found
                        Error executing cl.exe.
                        -error C2447: missing function header (old-style formal list?)
                        Hi,

                        Remove your while loop.

                        Code:
                        { while(1)
                        {
                        switch(ch) {
                        case'a' :
                        result=number1+numder2;
                        break;
                        case's' :
                        result=number1-number2;
                        break;
                        case'm' :
                        result=number1*number2;
                        break;
                        case'd' :
                        result=number1/number2;
                        break;
                        default:
                        printf("please enter the correct character");
                        break;
                        } /*end of while*/
                        Regards,
                        M.Sivadhas.

                        Comment

                        Working...