Hi again and thanks for the help!

PROBLEM: Only every other number entered by the user is registered by my program, except the last number, which is always registered. How can I get the program to register every line? And what is causing it to skip an input line in the first place?

I can't explain it, and I've tried a number of things from moving around where the cin>>x; input is located to using cin.ignore() ; and getlines. It hasn't worked.

/*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.*/

#include <iostream>

using namespace std;

int main ()
{
	double x;
	double max=0.0;
	double min=100.0; 
	double count = 0.0;
	double sum = 0.0;

	while(cin>>x)
	{
		cin>>x;

		if (x>=0.0 && x<= 100.0)
		{
			if(x>max)
			{
				max=x;
			}
			if (x<min)
			{
				min=x;
			}
			count++;
			sum = x + sum;
		}
		else 
		{
			cout<<"out of range; ignored."<<endl;
		}
	}
	
	cout<<"max= "<<max<<endl;
	cout<<"min= "<<min<<endl;
	cout<<"count= "<<count<<endl;
	cout<<"sum= "<<sum<<endl;


	double avg = (sum)/count;
	double range = max - min;
	
	cout<<"The average is "<<avg<<endl;
	cout<<"The range is "<<range<<endl;
}

You seem to have two cin>>x in your code at lines 23 and 21. I would delete line 23.

I might make count and integer but that is minor.
you have remembered to initialize your variables which is good.

while(cin>>x)
	{
		cin>>x;

while(cin>>x) does TWO functions. One is to evaluate if the cin is good and another is actually executing that cin >> x so only by typing that while you already got the user input so the second cin has to be deleted.

Neithan and StuXYZ; thanks to the both of you. The extra cin>>x making it skip makes sense. I'll take that out ASAP. Thanks Neithan especially for the extra info about how the 'while' function worked. I didn't realize the while would actually execute the cin>>x.

Thanks guys!

Neithan and StuXYZ; thanks to the both of you. The extra cin>>x making it skip makes sense. I'll take that out ASAP. Thanks Neithan especially for the extra info about how the 'while' function worked. I didn't realize the while would actually execute the cin>>x.

Thanks guys!

I explained to you that way because i had the same understanding problem and is something that had been a pain in the ass till somebody told me that and made me happy lol.

It's the same thing as for example if (variable++ == blah)... you know, it compares if adding one to variable is equal to blah...but it's actually changin variable's value to plus one! So that is similar to while and also gave me headaches till i discovered it.

So i hope i saved you some pain in the ass! hahah

Just out of curiosity, how do you stop the while loop with control+z?

--drjay

Actually I figured it out. ctrl+d in linux and ctrl+z in windows!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.