C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2009, 08:10 PM   #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;
	
	
}
Woot is offline   Reply With Quote
Old 06-30-2009, 08:17 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 06-30-2009, 08:32 PM   #3
Registered User
 
Join Date: Jun 2009
Posts: 4
how do i fix this then?
Woot is offline   Reply With Quote
Old 06-30-2009, 09:08 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Remove the extra read statements?
tabstop is offline   Reply With Quote
Old 06-30-2009, 09:16 PM   #5
Registered User
 
Join Date: Jun 2009
Posts: 4
which ones are the extra read statment
Woot is offline   Reply With Quote
Old 06-30-2009, 09:30 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-01-2009, 07:23 AM   #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!
legit is offline   Reply With Quote
Old 07-01-2009, 08:00 AM   #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?
WatchTower is offline   Reply With Quote
Old 07-01-2009, 08:15 AM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,359
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-01-2009, 08:58 AM   #10
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
FYI, from the forum guidelines:
Quote:
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.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 07-02-2009, 01:56 PM   #11
Registered User
 
Join Date: Jun 2009
Posts: 4
can you delete a board post?
Woot is offline   Reply With Quote
Old 07-02-2009, 02:15 PM   #12
Super Moderator
 
Join Date: Sep 2001
Posts: 4,680
We generally don't delete posts because they can be useful for future readers. We'll usually delete inappropriate comments, though.
sean is offline   Reply With Quote
Reply

Tags
c++, problem

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 05:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22