By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

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

P: 8
what am i missing here?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. // Declare Variables
  9. int sum;
  10. int devide;
  11. int add;
  12. int number1;
  13. int number2;
  14. int number3;
  15. int number4;
  16. int number5;
  17. int A = 90 < 110;
  18. int B = 80 < 90;
  19. int C = 70 < 80;
  20. int D = 60 < 70;
  21. int E = 0 < 60;
  22.  
  23. // FIgure your avarage letter grade
  24. cout << "To figure your average letter grade /n";
  25. cout << "Enter one grade: /n";
  26. cin >> number1;
  27. cout << "Enter second grade: /n";
  28. cin >> number2;
  29. cout << "Enter third grade: /n";
  30. add >> number1 + number2
  31. cin >> number3;
  32. cout << "Enter forth grade: /n";
  33. cin >> number4;
  34. cout << "Enter fifth grade: /n";
  35. cin >> number5;
  36.  
  37. // Add all numbers together
  38. add >> number1 + number2 + number3 + number4 + number5; 
  39.  
  40. //Display Total is
  41. cout << "Add is" << add << endl;
  42.  
  43. system("pause");
  44. return 0;
  45. }
Sep 21 '10 #1

✓ answered by Banfa

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.

Share this Question
Share on Google+
10 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 22 '10 #2

Expert 100+
P: 2,396
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.
Sep 22 '10 #3

P: 8
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
Sep 22 '10 #4

P: 8
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. int main()
  6.  
  7. {
  8. const int A = 1; 
  9. const int B = 2;
  10. const int C = 3;
  11. const int D = 4;
  12. const int E = 5;
  13.  
  14.  
  15. // Declare Variables
  16. int sum;
  17. int devide;
  18. int add;
  19. int number1;
  20. int number2;
  21. int number3;
  22. int number4;
  23. int number5;
  24. int number7;
  25.  
  26.  
  27. // Figure your avarage grade
  28. cout << "To figure your average grade /n";
  29. cout << "Enter one grade: /n";
  30. cin >> number1;
  31. cout << "Enter second grade: /n";
  32. cin >> number2;
  33. cout << "Enter third grade: /n";
  34. cin >> number3;
  35. cout << "Enter forth grade: /n";
  36. cin >> number4;
  37. cout << "Enter fifth grade: /n";
  38. cin >> number5;
  39.  
  40. // Add all 5 numbers together
  41.  (number1 + number2 + number3 + number4 + number5); 
  42.  
  43. //Display sum
  44. cout << "Sum is: /n" << number1 + number2 + number3 + number4 + number5;
  45.  
  46. //Display average
  47.  
  48. cout << "Total grade average is /n:" << ((number1 + number2 + number3 + number4 + number5) / 5);
  49.  
  50.  
  51. system("pause");
  52. return 0;
  53. }
Sep 22 '10 #5

Oralloy
Expert 100+
P: 983
Well, a series of ifs can do it for you. Something like this?

Expand|Select|Wrap|Line Numbers
  1. if (add >= 90) grade = A;
  2. else if (add >= 80) grade = B;
  3. ... etc ...
Sep 22 '10 #6

P: 8
this is not working right it keeps saying need to have ; and else if does not match the if


Expand|Select|Wrap|Line Numbers
  1. //Display average
  2. cout << "Total grade average is /n:" << (number1 + number2 + number3 + number4 + number5) / 5; average;
  3.       if (average >= 90) grade = A;{
  4.       cout << "A /n";
  5.       cout << "GREAT JOB!!! /n";
  6.       }
  7.       else (average >= 80) grade = B;{
  8.       cout << "B /n";
  9.       cout << "NICE WORK!!! /n";
  10.       }
  11.       else if (average >= 70) grade = C;{
  12.       cout << "C /n";
  13.       cout << "Good Work! /n";
  14.       }
  15.       else if (average >= 60) grade = D;{
  16.       cout << "D /n";
  17.       cout << "WORK HARDER YOU CAN DO IT! /n";
  18.       {
  19.       else if (average < 60) grade = F;{
  20.       cout << "F /n";
  21.       cout << "YOU NEED TO WORK EVEN HARDER!!! /n";
  22.       }
  23. system("pause");
  24. return 0;
  25. }
Sep 22 '10 #7

P: 1
my problem is that cout is an object ,how it is defined .it is an object of class ostream.
Sep 22 '10 #8

P: 1
// Add all 5 numbers together (Don't need to to this)
(number1 + number2 + number3 + number4 + number5);
Sep 22 '10 #9

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 22 '10 #10

Oralloy
Expert 100+
P: 983
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:
Expand|Select|Wrap|Line Numbers
  1.       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!
Sep 22 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.