I'm almost finished with a program that asks the user to input a number from 0 to 100, then asks if they'd like to enter another umber (y/n), and the loop continues until the user enters 'n'. At the end of the program, the sum, average, largest and smallest of the inputted numbers is to be shown. Also, if the user enters a number that isn't in the range of 0 to 100, the message "Please enter a number from 0 to 100" should appear. I'm having difficulty having that message appear, as well as obtaining the correct sum, average, largest and smallest of the inputted numbers. I'm not quite sure where I'm going wrong.
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
int number = 0;
int count = 0;
char key;
int sum = 0;
float average = 0;
int largest = 0;
int smallest = 0;
do
{
cout << "Enter a number between 0 and 100: ";
cin >> number;
count++;
cout << "Would you like to enter another number (y or n)? ";
cin >> key;
if (number < 0 || number > 100)
{
cout << "Please enter a number between 0 and 100. ";
cin >> number;
}
} while (key != 'n');
sum = sum + count;
average = float(sum)/number;
if (number > largest)
largest = number;
if (!count || smallest > number)
smallest = number;
cout << "You've entered " << count << " numbers. " << endl;
cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;
getch();
return 0;
}
do
{
cout << "Enter a number between 0 and 100: ";
cin >> number;
while (number < 0 || number > 100)
{
cout << "Please enter a number between 0 and 100. ";
cin >> number;
}
count++;
cout << "Would you like to enter another number (y or n)? ";
cin >> key;
} while (key != 'n');
This is an example my teacher gave of how the program should run:
Enter a number: 80
Would you like to enter another number (y or n)? y
Enter a number: 100
Would you like to enter another number (y or n)? y
Enter a number: 200
The number must be in the range 0 to 100.
Enter a number: -5
The number must be in the range 0 to 100.
Enter a number: 50
Would you like to enter another number (y or n)? y
Enter a number: 70
Would you like to enter another number (y or n)? n
You entered 4 numbers
The sum is 300
The average is 75.0
The largest is 100
The smallest is 50
int number = 0;
int count = 0;
char key;
do
{
cout << "Enter a number: ";
cin >> number;
while (number < 0 || number > 100)
{
cout << "The number must be in the range 0 to 100.\n";
cout << "Enter a number: ";
cin >> number;
}
count++;
cout << "Would you like to enter another number (y or n)? ";
cin >> key;
} while (key != 'n');
cout << "You've entered " << count << " numbers\n";
Actually you really should put in some effort and post your code snippets and then we try to help else I will be accused of providing solutions in this forum and get banned thereafter.
int number = 0;
int count = 0;
char key;
int sum = 0;
float average = 0;
int largest = -1;
int smallest = -1;
do
{
cout << "Enter a number: ";
cin >> number;
while (number < 0 || number > 100)
{
cout << "The number must be in the range 0 to 100.\n";
cout << "Enter a number: ";
cin >> number;
}
sum += number;
if (largest == -1) largest=number;
elseif (number > largest) largest=number;
if (smallest == -1) smallest=number;
elseif (number < smallest) smallest=number;
count++;
cout << "Would you like to enter another number (y or n)? ";
cin >> key;
} while (key != 'n');
cout << "You've entered " << count << " numbers\n";
cout << "The average is " << (sum/count) << "\n";
cout << "The largest is " << largest << "\n";
cout << "The smallest is " << smallest << "\n";
Take a look at this. I think that both input statements (cin >> x) should be embedded within a while loop that prints an error message and clears if there is invalid or out of range input. It is critical to check for errors otherwise fat fingering a key will cause the program to fail. It is very simple to fix this and the pattern is shown in an example within the following FAQ. http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3