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

Help With Looping In C++

P: 4
hey,
im learning basic C++ courses and wanted some help with this problem. Plz help
Thanks,
Anand

Q10
Message: Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater
than zero, the sum of all the numbers less than zero (which will be a negative number or zero),
and the sum of all the numbers, whether positive, negative or zero. T
he user enters the ten numbers just once each and the user can enter them in any order.
Your program should not ask the user to enter the positive numbers and the negative numbers separately.


I have done this so far...
Expand|Select|Wrap|Line Numbers
  1. int main()
  2.     double sum_greater_than_zero = 24; 
  3.     double sum_less_than_zero =0;
  4.     double sum_of_all = 0 ;
  5.     int number ;
  6.  
  7.     cout << "Please press return after entering a value. \n ";
  8.     cout << "What number would you like to pick? \n ";
  9.     cin >> number;
  10.     cout << "Thank You ";
  11.  
  12.     while ( number != 0 )
  13.     {
  14.                    sum_greater_than_zero *= number;    
  15.       cout << "The sum is: " << sum_greater_than_zero++ << endl;
  16.     }
  17.                    if ( number > 0 )
  18.             sum_greater_than_zero += number;
  19.  
  20.         else if ( number < 0 )
  21.              sum_less_than_zero += number;
  22.  
  23.  
  24.  
  25.         return 0;
  26.   }
  27.  
Plz help
Anand
Feb 25 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 1,510
a couple of things which look wrong (see lines with << )
Expand|Select|Wrap|Line Numbers
  1. int main()
  2.     double sum_greater_than_zero = 24; 
  3.     double sum_less_than_zero =0;
  4.     double sum_of_all = 0 ;
  5.     int number ;
  6.  
  7.     cout << "Please press return after entering a value. \n ";
  8.     cout << "What number would you like to pick? \n ";
  9.     cin >> number;
  10.     cout << "Thank You ";
  11.  
  12.     while ( number != 0 )
  13.     {
  14.                    sum_greater_than_zero *= number;    
  15.       cout << "The sum is: " << sum_greater_than_zero++ << endl;
  16.     }  <<  i think this is in the wrong place
  17.                    if ( number > 0 )
  18.             sum_greater_than_zero += number;
  19.  
  20.         else if ( number < 0 )
  21.              sum_less_than_zero += number;
  22.  
  23. << read next number here 
  24.  << put a } here
  25.  
  26.  
  27.         return 0;
  28.   }
  29. }
Feb 25 '07 #2

P: 4
it says illegal else without matching if...
anand
Feb 25 '07 #3

P: 27
Expand|Select|Wrap|Line Numbers
  1. //Some code has been removed...this is not a functional program!
  2.  
  3. int main()
  4.     cout << "Please press return after entering a value. \n ";
  5.     cout << "What number would you like to pick? \n ";
  6.     cin >> number;
  7.     cout << "Thank You ";
  8.  
  9.     while ( number != 0 )
  10.     {
  11.            // operations...
  12.               cin>>number;
  13.  
  14.     }  
  15.  
  16.   }
This way, the program will terminate when the use enters zero value!!
check ur while loop condition
Feb 25 '07 #4

P: 4
I tried it and the program works this way
" it asks me to pick a number and then it just displays thank you."
it doesn't perform any operations"

Thanx
Anand
Feb 25 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
The program is waiting for another number. The code given above does not display a prompt inside the loop, so the loop will execute until a 0 is pressed.
Feb 25 '07 #6

P: 4
can someone jus make changes in the program ...u can read the question on top...plzzzz its eating my head!
ANand
Feb 25 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
No. You will have to write your own program - the experts at this site will not help you cheat. We are more than willing to help you, but we have given you quite enough help in this thread for you to solve the problem on your own - so do it.
Feb 25 '07 #8

Post your reply

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