Thread: Help Help Help!!!

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Help Help Help!!!

    I have a problem i can't seem to solve
    Combining While Loops and If Statements
    Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the
    range.
    Notes:
    • You do not know ahead of time how many numbers will be in the list.
    • When you want to stop entering numbers, enter control‐Z.
    • The range is the difference between the lowest and the highest number.
    • The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range.

    this is my prompt so far. The average is only the last number not all the cins combine between 0 and 100

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    	double n, sum = 0, count = 0;
    	
    	while(cin >> n)
    	{cin >> n;
    		if  ((n >= 0.0) && (n <= 100.0))
    		{ 
    			++count;
    			sum += n;	
    		}
    
    		else if (n == '^Z') break;
    		
    		else
    		{
    			cout << "out of range: ignored.\n";
    		}
    	}
    	
    	cout << "The average is " << sum / count << endl;
    	
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got too many read statements. (Edit: That is to say, you've got a read statement in your while condition, plus one at the top of the loop.)
    Last edited by tabstop; 06-30-2009 at 08:23 PM.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    how do i fix this then?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remove the extra read statements?

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    which ones are the extra read statment

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Woot View Post
    which ones are the extra read statment
    So, trace through your program by hand. Take a list of numbers as input, say "1 47 36 29 86 55 ^Z" and see what happens as your program executes.

  7. #7
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Do you know what your read statements are? I somehow come to the conclusion that you haven't been paying attention in class. :P
    MSDN <- Programmers Haven!

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    The question is do you know which one is in the expression and which one is not?
    Which one are you using to terminate the loop and which one is not used to terminate the loop?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    else if (n == '^Z') break;
    The purpose of the instruction to "enter control‐Z" is to simulate end of file, and when end of file is encountered, (cin >> n) evaluates to false, terminating the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    FYI, from the forum guidelines:
    Tips for Efficient and Successful Posting

    1. Don't use all caps.

    2. Use descriptive subject lines. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
    Help Help Help!!! does not qualify as a descriptive subject line.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    can you delete a board post?

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    We generally don't delete posts because they can be useful for future readers. We'll usually delete inappropriate comments, though.

Popular pages Recent additions subscribe to a feed

Tags for this Thread