Hey guys I needed some help on this problem that we have to do for our introductory computer science class

PROBLEM:
Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the
range.

• 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.

My solution so far... it sucks but I have no idea what I am doing:

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string answer = "";
double counter = 1.11;
int c;
while ((counter >= 0) && (counter <= 100))
{
cout << counter;
cin >> answer;
if (answer == "^Z")
break;
counter++;
}
if ((answer < 0) || (answer > 100)
	cout << "out of range; ignored." << endl;
		cout << "" << endl;
}

PLEASE HELP!

Recommended Answers

All 2 Replies

Let's break down your problem step by step:

PROBLEM:
Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the
range.

• You do not know ahead of time how many numbers will be in the list.

At this point in your illustrious programming career, you obviously do not have the tools to handle "infinite" amounts of input. What your professor is trying to do is to have you make "assumptions" in your program... and you as the programmer have to be able to back up your assumptions with logical arguments.

One thing you can do though, is to provide mechanisms to advise the user that your program cannot handle anymore input (should it reach that point.)

• When you want to stop entering numbers, enter control‐Z.

Here is a good tutorial on using "control+z"
http://cis.stvincent.edu/carlsond/swdesign/basic/streams.html


• The range is the difference between the lowest and the highest number.

You can start determining highest/lowest number as soon as you start accepting user input. Use boolean logic to determine if the number entered is higher than the highest previous entry or lower than the lowest previous entry. If it meets either one of these conditions, store the new number in its respective place. If not, continue.


• The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range.

Part of the robustness of your program is to check user input for validity.. because humans make mistakes. When you start accepting input from the user, test the input to see if it is within' the acceptable domain. If not, you could easily just prompt for another entry with total disregard to the irrelevant data.


Hope this will get you headed in the right direction.

Ok, I would currently say that your solution is INCOMPLETE, But it doesn't suck!! :)

Though I will not completely give you the solution, here are a few tips on getting to the right path.

Firstly , Try Implementing the solution considering that the exit will be at the entry of -1.After you have got that accomplished , then you can come up with a way to take in a string input. check if its the exit status, if its not ...... go on and convert the string into a double.... and perform the later operations.

A little more work to do and i bet you will have the assignment DONE. Good luck.. Come back if you have more queries... but bring your code along with you ! :)

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.