Help With Looping In C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anand Churi
    New Member
    • Feb 2007
    • 4

    Help With Looping In C++

    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...
    Code:
    int main()
    { 
        double sum_greater_than_zero = 24; 
        double sum_less_than_zero =0;
    	double sum_of_all = 0 ;
    	int number ;
    
    	cout << "Please press return after entering a value. \n ";
        cout << "What number would you like to pick? \n ";
    	cin >> number;
    	cout << "Thank You ";
    		
    	while ( number != 0 )
    	{
                       sum_greater_than_zero *= number;	
    	  cout << "The sum is: " << sum_greater_than_zero++ << endl;
    	}
                       if ( number > 0 )
    			sum_greater_than_zero += number;
    		
    		else if ( number < 0 )
    		     sum_less_than_zero += number;
      		 
    
    	
    		return 0;
      }
    Plz help
    Anand
    Last edited by horace1; Feb 25 '07, 05:28 PM. Reason: added code takes
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    a couple of things which look wrong (see lines with << )
    Code:
    int main()
    { 
        double sum_greater_than_zero = 24; 
        double sum_less_than_zero =0;
    	double sum_of_all = 0 ;
    	int number ;
    
    	cout << "Please press return after entering a value. \n ";
        cout << "What number would you like to pick? \n ";
    	cin >> number;
    	cout << "Thank You ";
    		
    	while ( number != 0 )
    	{
                       sum_greater_than_zero *= number;	
    	  cout << "The sum is: " << sum_greater_than_zero++ << endl;
    	}  <<  i think this is in the wrong place
                       if ( number > 0 )
    			sum_greater_than_zero += number;
    		
    		else if ( number < 0 )
    		     sum_less_than_zero += number;
      		 
    << read next number here 
     << put a } here
    
    	
    		return 0;
      }
    }

    Comment

    • Anand Churi
      New Member
      • Feb 2007
      • 4

      #3
      it says illegal else without matching if...
      anand

      Comment

      • bravephantom
        New Member
        • Dec 2006
        • 27

        #4
        Code:
        //Some code has been removed...this is not a functional program!
        
        int main()
        { 
        	cout << "Please press return after entering a value. \n ";
            cout << "What number would you like to pick? \n ";
        	cin >> number;
        	cout << "Thank You ";
        		
        	while ( number != 0 )
        	{
                   // operations...
                      cin>>number;
        
        	}  
        
          }
        This way, the program will terminate when the use enters zero value!!
        check ur while loop condition
        Last edited by Ganon11; Feb 25 '07, 07:43 PM. Reason: Removed irrelevant code to prevent copy/pasting

        Comment

        • Anand Churi
          New Member
          • Feb 2007
          • 4

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

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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.

            Comment

            • Anand Churi
              New Member
              • Feb 2007
              • 4

              #7
              can someone jus make changes in the program ...u can read the question on top...plzzzz its eating my head!
              ANand

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                Working...