When the user entera a non number into a integer variable the stream will fail. Look at line 19 in your code that should give you a idea what to do.
I would also suggest reading up on how streams work in C++ since they are very handy to know about.
Here is a quick example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
int main()
{
int number;
int sum = 0;
cout << "Enter your numbers or a non number to stop!" << endl;
// The condition is saying keep the loop running
// until cin >> number is not true. So since when we
// enter anything but a number into a integer the stream break
// the condition will be false since we are no longer able to read
// numbers into the variable.
while (cin >> number)
{
sum += number;
}
cout << sum << endl;
}
|
You are also going to have to take a look at how you want your program to run again. Remember that you will need to find the max number entered, min number enters and the means.
What I do is before I start to code anything I grab a notepad or go into a text editor and write down all the requirements of my program. So your would look like this
1) Find sum
2) Find max
3) Find min
ect ect.
I then write down anything I will need to do that requirement, or any ideas I have for it. Like this.
1) Find sum: Need to have a variable to store the sum of all the numbers in. When inputting the numbers just += the inputted number to sum IE (sum += number).
2) Find max: Need a variable to hold the largest number entered. To find this largest number I need conditional operators to compare all the numbers entered so far.
ect. ect.
For large projects its also good to break the problem down into little managable problems and then make a plan of how to go through it like this.
Problem: Get each students name, age, and grade from their test. Then find the median of the students grades.
1) Create a class type that will be used for each student.
2) Get input from the user about each student. I'm going to use a vector<Student> to hold all of the students
3) Sort the vector<Student> by their grades. Use the std::sort() function
4) Find the median of the grades.
Now obviously that is a very simplified version of it but it shows how it can help. That way before I every start coding I know exactly what I need to do step by step, what variables I will need and the tools I need. So I go in with a plan which makes it much easier to solve each problem.