Anyone know how to accomplish this task on visual studio with C++?

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.

>>Anyone know how to accomplish this task on visual studio with C++?
Yes. Do you? Hint: start out the program very simply and prompt for input. When that is done put that in a loop. Compile and correct all errors again. After that, add code to implement the rest of the requirements.

No. I definitely do not know how to do it. I will try my best with your given advice and post my results later. Thanks again Ancient Dragon!

>>No. I definitely do not know how to do it
That's why you are in school -- to learn how :)

Ok. I honestly have no idea where to begin. I seem to be having a mental block. Any help would be greatly appreciated.

Start here

#include <iostream>
using namespace std;

int main()
{
   // put your code here

}

Re-read my previous post #2 and code one sentence at a time. Code the line that displays the prompt. Get that to work and compile, then code another line that asks for user input.

Have you read your textbook and done the example programs at the end of each chapter? If not, then you should do that.

Ok. I have been trying. I am unsure of how to prompt the user to give input without asking a question or entering a statement. This is the 17th assignment and I have been breezing through until now. Can someone please get me started so I can visually see what to do?

"to prompt" just smply means use cout to display the message cout << "Enter something\n"; and to get input for a string. You need to STUDY YOUR TEXTBOOK for other variations of cin and cout.

std::string myname;
cout << "Enter your name\n";
getline(cin, myname);

There is no textbook. There are some readings online for class but they are no help. We are given these assignments and are not even taught how to do them. Like I said, I have been having no trouble at all until now. We were not taught how to take infinite values until the user enters ctrl-Z. I really am trying hard to do this. I am completely stumped.

>>There is no textbook.
What the hell :-O Are you doing this in some sort of school ? or learning c++ all on your own? I assumed you were in school, but maybe I was wrong.


What part(s) of the assignment don't you know how to do. cin or cout , or something else

I'm sorry, but we can't give you a complete course in c++. If you are on your own then I'd strongly suggest you spend some $$$ and buy a good c++ book that will teach you.

I am at a university. It is an intro class. I understand the basics but I don't understand this problem.

>>I am at a university.

Quit. Go to some other university because that one obviously will teach you nothing.

Do you know what the mean is and how to calculate it? How about the range? If not, then google for those terms.

And like I said before, take it one small step at a time. As someone once said "use the Divde And Conquor" method to solve the problem.

I know what mean and range are. I do not know how to do the problem and how to define these values based on repeated user input. Thanks for the help but I am at a dead end.

I thought you said you know the basics. But apparently you don't.

int main()
{
   float mean = 0;
   float range = 0;
   float num = 0;
   float sum = 0;
   int NumberInputs = 0;
   // infinite loop
   for(;;)
   {


   }
}

Ok. I have been trying. I am unsure of how to prompt the user to give input without asking a question or entering a statement. This is the 17th assignment and I have been breezing through until now. Can someone please get me started so I can visually see what to do?

No one gives an assignment like this as the first assignment, so I assume you already know how to use cin and cout and store things in variables. I assume you had to do that in at least some of those sixteen earlier assignments. Also, no one assigns an assignment where you don't know how many elements there are going to be without first teaching you about arrays where you do know how many elements there are going to be, so I assume you know how to use arrays at least a little bit and you know how to use cin and cout, but you have not shown any code.

I know what mean and range are. I do not know how to do the problem and how to define these values based on repeated user input. Thanks for the help but I am at a dead end.

Since you don't know how many elements there are going to be in advance, you need to either use an array that is bigger that the largest possible number of elements or you need to use a dynamic array or you need to use something like a vector. A vector would be the easiest in my opinion.

To start off, don't worry about calculating anything. Just give the user instructions, read in a value, push the value onto the vector, check whether the user is done, and if not, repeat. Then display the list.

http://www.cplusplus.com/reference/stl/vector/

You'll be using the push_back function and the [] operator to read in and access the information. The size function will come in handy too. I don't think you'll need anything else from the vector class template. That should get you started. Give it a try, post some code, and go from there.

>>and if not, repeat. Then display the list.
Displaying the list is not a requirement as the OP posted.

You could use an array, but its not necessary because there is no need to keep all the input values. Just sum them up as they are entered. You also need another counter to keep count of the number of entries the user entered. After all done, then just do the simple math.

This is really a very simple program that requires no (zero) advanced things such as <vector>, arrays or memory allocations. Don't make this more difficult than is needed.

commented: Good observation. +8

>>and if not, repeat. Then display the list.
Displaying the list is not a requirement as the OP posted.

You could use an array, but its not necessary because there is no need to keep all the input values. Just sum them up as they are entered. You also need another counter to keep count of the number of entries the user entered. After all done, then just do the simple math.

This is really a very simple program that requires no (zero) advanced things such as <vector>, arrays or memory allocations. Don't make this more difficult than is needed.

Yes, AD, you are correct. I didn't read the problem description closely. I thought all of the values had to be displayed, but they don't. To the OP, disregard my vector recommendation. AD is correct. No need to make it harder than it needs to be.

Step 1 - a loop which reads numbers and prints them out (this should be basically the same as a previous assignment)
Step 2 - make it exit on ctrl-z
Step 3 - make it reject out of range numbers
Got the idea yet?

Think about what steps you can
a) acomplish
b) might need to do first

When you've got somewhere (and got stuck), then post back.

Don't just stare at your "elephant" and complain that it's too big to eat all at once.

Here is my start. I want the user to be able to enter as many inputs as he wants but also be able to end them with ctrl-z. Right now, if I run it and enter '2', it prints 2, 2, press enter to continue.

#include <iostream>
using namespace std;

int main()
{
  double n;
  cin >> n;

  while (n>0) 
  {
    cout << n << endl;
	return 0;
}
}

Try re-examining the flow of your program. Think as the compiler: go line by line and ask "What is happening here?"

If you do not understand something in your code, I recommend you look it up on www.cplusplus.com

Ok. I think I am finally getting somewhere after hours of practice:

#include <iostream>
using namespace std;

int main()
{
  double n;
  double min = 0;
  double max = 0;
  double count = 0;
  double sum = 0;
  double range;
  
  for (;;)
  {
	cin >> n;
	if ((n < 0)||(n > 100)) break;
	++count;
	sum += n;

	if (count == 0)
{
	max = n;
    min = n;
}
	if (n > max)
{
	max = n;
}
	if (n < min)
{
    min = n;
}
	range = max - min;

	cout << "The average is " << sum/count << endl;
	cout << "The range is " << range << endl;
  }
}

It is only letting me enter one value and then it gives the range and average. I need to enter as many as I want and then enter ctrl-z to calculate.

I got it to execute with a control-Z. But, the range and average are wrong. Any ideas?

#include <iostream>
using namespace std;

int main()
{
  double n;
  double minval = 0;
  double maxval = 0;
  double count = 0;
  double sum = 0;
  double range;
  
  do
  {
	cin >> n;

	if ((n < 0)||(n > 100)) continue;
	++count;
	sum += n;

	if (count == 0)
{
	maxval = n;
    minval = n;
}
	if (n > maxval)
{
	maxval = n;
}
	if (n < minval)
{
    minval = n;
}
{
	range = maxval - minval;
}
  }while(! cin.fail());
	{
	cout << "The average is " << sum/count << endl;
	cout << "The range is " << range << endl;
	}
  
}

I got it to execute with a control-Z. But, the range and average are wrong. Any ideas?

#include <iostream>
using namespace std;

int main()
{
  double n;
  double minval = 0;
  double maxval = 0;
  double count = 0;
  double sum = 0;
  double range;
  
  do
  {
	cin >> n;

	if ((n < 0)||(n > 100)) continue;
	++count;
	sum += n;

	if (count == 0)
{
	maxval = n;
    minval = n;
}
	if (n > maxval)
{
	maxval = n;
}
	if (n < minval)
{
    minval = n;
}
{
	range = maxval - minval;
}
  }while(! cin.fail());
	{
	cout << "The average is " << sum/count << endl;
	cout << "The range is " << range << endl;
	}
  
}

Add the following line in red to your first if-statement. Make sure it displays. If not, then that code is never being executed.

if (count == 0)
    {
        cout << "count = 0" << endl;
    	maxval = n;
        minval = n;
    }

i changed the count to count = -1. now the count works. But, the sum is wrong. It is adding the last value entered twice. For example, if I enter 1, then 2, then 3, the sum is calculated as 9 instead of 6.

#include <iostream>
using namespace std;

int main()
{
  double n;
  double minval = 0;
  double maxval = 0;
  double count = -1;
  double sum = 0;
  double range;
  
  do
  {
	cin >> n;

	if ((n < 0)||(n > 100)) continue;
	++count;
	sum += n;

	if (count == 0)
{
	maxval = n;
    minval = n;
}
	if (n > maxval)
{
	maxval = n;
}
	if (n < minval)
{
    minval = n;
}
{
	range = maxval - minval;
}
  }while(! cin.fail());
	{
	cout << sum  << endl;
	cout << count  << endl;
	cout << "The average is " << sum/count << endl;
	cout << "The range is " << range << endl;
	}
  
}

i changed the count to count = -1. now the count works. But, the sum is wrong. It is adding the last value entered twice. For example, if I enter 1, then 2, then 3, the sum is calculated as 9 instead of 6.

#include <iostream>
using namespace std;

int main()
{
  double n;
  double minval = 0;
  double maxval = 0;
  double count = -1;
  double sum = 0;
  double range;
  
  do
  {
	cin >> n;

	if ((n < 0)||(n > 100)) continue;
	++count;
	sum += n;

	if (count == 0)
{
	maxval = n;
    minval = n;
}
	if (n > maxval)
{
	maxval = n;
}
	if (n < minval)
{
    minval = n;
}
{
	range = maxval - minval;
}
  }while(! cin.fail());
	{
	cout << sum  << endl;
	cout << count  << endl;
	cout << "The average is " << sum/count << endl;
	cout << "The range is " << range << endl;
	}
  
}

You need to figure out how many times you are going through the loop when you enter, say, 5 numbers. If you are going through the loop 6 times instead of 5, then you are probably executing code that you don't want to when Control-Z is entered, so you need to rethink your loop design or make sure that the code you want to execute only when a legal number is entered is NOT executed when Ctrl-Z is entered.

I probably has to do with the functionality of cin.fail(). Change your loop to something different. Maybe something like this;

bool more = true;
char flag;
while(more)
{
   cin >> n;

   //do dah

   cout << "do you want to enter another value. Enter y for yes and n for no" << endl;
   cin >> flag;
   if(flag == 'n')
     more = false;
}

Is there a quick way to make the program not add the last user input value twice. I have sum += n. I do not understand why it is not working.

I got it. Does it look ok? I ran it and it works. I just redefined sum as sum = sum - n towards the end. Now, I just need to add the statement "out of range; ignored" if a value outside of 0 to 100 is entered. Where can I place this statement without ruining the program?

#include <iostream>
using namespace std;

int main()
{
  double n;
  double minval = 0;
  double maxval = 0;
  double count = -1;
  double sum = 0;
  double range;
  
  do
  {
	cin >> n;
	++count;
	sum += n;

	if (count == 0)
{
	maxval = n;
    minval = n;
}
	if (n > maxval)
{
	maxval = n;
}
	if (n < minval)
{
    minval = n;
}
{
	range = maxval - minval;
}
  }while(! cin.fail());
	{
	sum = sum - n;
	cout << "The average is " << sum / count << endl;
	cout << "The range is " << range << endl;
	}
  
}

Well, let's say that you enter the sequence 1, 2, 3, and then the fourth time through the loop you enter ctrl-z to get out of the loop. The question becomes does ctrl-z get you out of the loop without processing the rest of the loop before the while conditional and then stops when the stream fails, or does it pop you out of the loop without passing through the rest of the loop first? If it's the former, then the last value entered will be added to sum, etc. You could put some output statements in the loop to try to find out what actually happens within your loop, or you could ditch the current loop conditionals and institute a more user friendly version of the loop using the concept of a flag as I demonstrated.

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.