Whats wrong with this why does it not add all the numbers together and display them

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • edithgingras
    New Member
    • Sep 2010
    • 8

    #1

    Whats wrong with this why does it not add all the numbers together and display them

    what am i missing here?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    // Declare Variables
    int sum;
    int devide;
    int add;
    int number1;
    int number2;
    int number3;
    int number4;
    int number5;
    int A = 90 < 110;
    int B = 80 < 90;
    int C = 70 < 80;
    int D = 60 < 70;
    int E = 0 < 60;
    
    // FIgure your avarage letter grade
    cout << "To figure your average letter grade /n";
    cout << "Enter one grade: /n";
    cin >> number1;
    cout << "Enter second grade: /n";
    cin >> number2;
    cout << "Enter third grade: /n";
    add >> number1 + number2
    cin >> number3;
    cout << "Enter forth grade: /n";
    cin >> number4;
    cout << "Enter fifth grade: /n";
    cin >> number5;
    
    // Add all numbers together
    add >> number1 + number2 + number3 + number4 + number5; 
    
    //Display Total is
    cout << "Add is" << add << endl;
    
    system("pause");
    return 0;
    }
    Last edited by Banfa; Sep 22 '10, 01:10 AM. Reason: Added [code]...[/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    OK for future questions you should include more text in the post explaining the problem and the title should be a summary. I am aware that the "Ask a Question" page does not make this clear.

    You problem is line 38 of your listing
    add >> number1 + number2 + number3 + number4 + number5;

    This is a valid numerical expression so it compiles (return the value of add shifted right by the sum of all the number variables).

    You need to use the assignment operator (=) to assign the sum of all the number variables to add.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      By the way, what do you expect lines 17-21 to do? These lines assign the result of a logical expression to those variables. Logical expressions can only have the values 0 or 1.

      Comment

      • edithgingras
        New Member
        • Sep 2010
        • 8

        #4
        yes i figured that all out thank you and as for the rest of the program I need to assign a letter grade to the outcome of the average grade. I made necessary changes by adding const int of A = 1 B=2 C=3 D=4 F=5
        the other ints have been removed and I need to place the variable of A=90+ B=80>90 C=70>80 D=60>70 and F=>60 where would this go and I am not sure of the formula to insert it. Should it be with a if statement or something else? Thanks for you help

        Comment

        • edithgingras
          New Member
          • Sep 2010
          • 8

          #5
          this is the updated version now I need to make the total grade average a letter grade based on the criteria in other post. Whats the best a way to incorperate this function

          Code:
          #include <iostream>
          #include <string>
          
          using namespace std;
          int main()
          
          {
          const int A = 1; 
          const int B = 2;
          const int C = 3;
          const int D = 4;
          const int E = 5;
          
          
          // Declare Variables
          int sum;
          int devide;
          int add;
          int number1;
          int number2;
          int number3;
          int number4;
          int number5;
          int number7;
          
          
          // Figure your avarage grade
          cout << "To figure your average grade /n";
          cout << "Enter one grade: /n";
          cin >> number1;
          cout << "Enter second grade: /n";
          cin >> number2;
          cout << "Enter third grade: /n";
          cin >> number3;
          cout << "Enter forth grade: /n";
          cin >> number4;
          cout << "Enter fifth grade: /n";
          cin >> number5;
          
          // Add all 5 numbers together
           (number1 + number2 + number3 + number4 + number5); 
          
          //Display sum
          cout << "Sum is: /n" << number1 + number2 + number3 + number4 + number5;
          
          //Display average
          
          cout << "Total grade average is /n:" << ((number1 + number2 + number3 + number4 + number5) / 5);
          	 
          
          system("pause");
          return 0;
          }
          Last edited by Banfa; Sep 22 '10, 08:10 AM. Reason: Added [code]...[/code] tags round the code. Please use them yourself in future.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Well, a series of ifs can do it for you. Something like this?

            Code:
            if (add >= 90) grade = A;
            else if (add >= 80) grade = B;
            ... etc ...

            Comment

            • edithgingras
              New Member
              • Sep 2010
              • 8

              #7
              this is not working right it keeps saying need to have ; and else if does not match the if


              Code:
              //Display average
              cout << "Total grade average is /n:" << (number1 + number2 + number3 + number4 + number5) / 5; average;
                    if (average >= 90) grade = A;{
              	  cout << "A /n";
              	  cout << "GREAT JOB!!! /n";
              	  }
              	  else (average >= 80) grade = B;{
              	  cout << "B /n";
              	  cout << "NICE WORK!!! /n";
              	  }
              	  else if (average >= 70) grade = C;{
              	  cout << "C /n";
              	  cout << "Good Work! /n";
              	  }
              	  else if (average >= 60) grade = D;{
              	  cout << "D /n";
              	  cout << "WORK HARDER YOU CAN DO IT! /n";
              	  {
              	  else if (average < 60) grade = F;{
              	  cout << "F /n";
              	  cout << "YOU NEED TO WORK EVEN HARDER!!! /n";
              	  }
              system("pause");
              return 0;
              }
              Last edited by Banfa; Sep 22 '10, 08:11 AM. Reason: Added [code]...[/code] tags round the code, please use the yourself in future.

              Comment

              • sant5
                New Member
                • Sep 2010
                • 1

                #8
                my problem is that cout is an object ,how it is defined .it is an object of class ostream.

                Comment

                • indexpert
                  New Member
                  • Sep 2010
                  • 1

                  #9
                  Ans:

                  // Add all 5 numbers together (Don't need to to this)
                  (number1 + number2 + number3 + number4 + number5);
                  Last edited by Banfa; Sep 22 '10, 08:12 AM. Reason: Removed code unrelated to answer

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    On line 7 in code of post 7 you are missing an if. I expect the compiler was producing an error at line 11. When you get compiler errors if the error is not immediately obvious on the line the compiler has highlighted then you need to learn to look back and the previous few lines of code.

                    Comment

                    • Oralloy
                      Recognized Expert Contributor
                      • Jun 2010
                      • 988

                      #11
                      Goodness, this is a little confused.

                      Yes, Banfa is right, you are missing an "if" on line 7.

                      Before that, though, line 3 (as well as 7 and others) is apparently malformed.

                      Did you mean to write something like:
                      Code:
                            if (average >= 90) { grade = A;
                      Where the opening brace started the consequent of the "if", and not an independent "block statement" following the "if"?

                      Good luck!

                      Comment

                      Working...