I want a user to enter as series of integers, then terminating the program when the a vale of =<0 is entered. Then I want to print out the first, last, largest and smallest value between all the values that the user entered. How do you determine the largest, smallest, first and last entry that the user did?
I'm going with a for loop (most likely) having my test stop at an entry =<0. I think I know my way around loops by now pretty well, but my problem is with determining these values so I know how to output them on the screen.
So the question is, what to put into "do something".
Option 1 is to add values into a list. Once the loop is over, you can find the interesting values from the list. You need a list that is either big enough to start with (hard to guess) or can grow dynamically (e.g. std::vector).
Option 2 is possible here. No list. You are in the "do something".
You don't know whether this is the first time, last time, or some other value.
Except you can know. If you stored the value as "first" and stored a flag that you have found first, that flag shows that you don't repeat that after the first time.
Every time is the last time. It overwrites the previous "last", which obviously is no longer the last.
Every time might be bigger or smaller than the largest or smallest so far. Just update as necessary.
I think I got the smallest/largest part figured out. I'm not sure about the flag for the first/last part. I'm looking at the "Flags section" in my book and it's very small and doesn't seem to relate to what you said.
If elaborating doesn't take a lot of your time, I would really appreciate it. Just for the first/last value part.
bool needFirst = true; // boolean "flag"
int first = 0;
int value = 0;
while ( std::cin >> value && 0 < value )
{
if ( needFirst )
{
first = value;
needFirst = false;
}
// other code
}
However, since you do know that all valid values are larger than 0, you do not need a separate flag variable. Do you see why?
I do not :(. I did however manage to write it down and it compiles and does the job perfectly. I just wanted to thank you for setting me in the right direction.
Really appreciate your help. Now I need to finish the last part of the assignment which sadly doesn't have a section in the book, so I'm off to a long night with research.
#include <iostream>
usingnamespace std;
int main()
{
int number, smallest, largest, firstEntry, lastEntry;
float theAverage, theSum, theCount;
cout << "Enter a number (negative number or 0 to terminate): " << endl;
cin >> number;
firstEntry = number;
largest = 1;
smallest = number;
theCount = 0;
theSum= number;
if (number > 0)
{
while (number>0)
{
++theCount;
lastEntry = number;
if (largest < number)
{
largest = number;
}
elseif (number < smallest)
{
smallest = number;
}
cout << "Enter a different number (negative number or 0 to terminate): " << endl;
cin >> number;
theSum +=number;
theAverage = theSum / theCount;
}
cout << "The largest number you entered was: " << largest << endl;
cout << "The smallest number you entered was: " << smallest << endl;
cout << "The first number you entered was: " << firstEntry << endl;
cout << "The last number you entered was: " << lastEntry << endl;
cout << "The number of times you entered a number were: " << theCount << endl;
cout << "The sum of the numbers you entered was: " << theSum << endl;
cout << "The average of the numbers you entered was: " << theAverage << endl;
}
elseif (number <= 0)
{
cout << "There are no statistics" << endl;
}
return 0;
}