Hi all I'm new here. I have an assignment I'm working on. User enters positive numbers, program totals, averages, find min and max and displays to the user and user enters a negative number to exit program and console displays sum, average, min and max. I got everything but the min part. For some reason its dropped numbers off and I cant figure out why. Please point me in the right direction.
#include <iostream> //include input and output library
int main() //main function
{ // beginning of main function
usingnamespace std; //using the standard namespace
int n; //counter for the total number of input values
float in_value; //variable to hold the current input value
float sum_of_in_values; //variable to hold the running total
float average_value; //variable for holding the average calculated
bool more; //flag to indicate whether the just input
float max; //variable for maximum user input number
float min; //variable for minimum user input number
// value is to be processed as one of the
// input values or it is just a value to
// indicate NO MORE
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
//initialize variables properly
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
more = true; //assume we have more input values to come in
n = 0; //number of entries initialized
in_value = 0.0; //user input variable initialized
sum_of_in_values = 0.0; //sum of user entries variable initialized
average_value = 0.0; // all values initialized
max = 0.0; // max intialized
min = 100000000.0; // min initialized
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// ask user for the first input value
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
cout << "Please enter a non negative number (<0 = no more): ";
cin >> in_value; //receive the value from user
if (in_value < 0) //if the input value signifies the end then
more = false; //turn the flag to false to avoid entering
// the major loop to do work
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
//The major loop to process each incoming value
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
while (more == true) //while this new input is an input value
{
n = n + 1; //one more valid number has been accepted
sum_of_in_values = sum_of_in_values + in_value;
if (max < in_value)
{
max = in_value;
}
cout << endl ; //Go to the next line on screen
//‐‐‐‐‐‐‐‐‐ask for the next input value before end of loop
cout << "Please enter the next non negagtive number (<0 = no more): ";
cin >> in_value; //receive value from user
if (in_value < 0)
more = false; //no more input values from user
if (in_value <= min)
{
if (in_value > 0) //sets minimum check
min = in_value; //to set minimum value we subtracted 1, sum_of_in_values = sum_of_in_values + in_value;
//this would add 1, so if we subtract 1 to compensate it will give an accurate min num
}
else
min = min;
} //end while
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// Now display the correct summary information
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
cout << endl; //skip a line for readability on the screen
if (n > 1) //if user input is more than 1 execute this
{
average_value = sum_of_in_values / n ; // calculate average of user input
cout << "\nThere is a total of " << n << " input values." << endl; // output total
cout << "The average is: " << average_value << endl; // output average on user input
cout << "The maximum value of your entries is: " << max << endl; // output of maximum numbers
cout << "The minimum value of your entries is: " << min << endl; // output of minimum numbers
cout << "The total sum of all input is: " << sum_of_in_values << endl; // output of the sum of user numbers
} //end if (n > 1)
else // n is <= 1
{
if ( n == 1) // begin if n == 1
{
cout << "Since you only keyed in one non negative number, the \n" ;
cout << "average is the input value: " << sum_of_in_values <<".\n";
cout << "The max is also the input value: " << max << endl;
} // end (if n == 1)
else //n is < 1
{
cout << "You have not input a valid entry. ";
cout << "I cannot calculate the average. Please try again.\n" << endl;
} //end else n is < 1
} //end else n is <= 1
cout << "\n\nBye!\n\n" << endl; //bye for exit and skip a line to make it clearer for the next time the program is ran
system("PAUSE"); //keep window showing results until user hits button to restart loop
return main(); //call to return to main function and rerun program until window is closed
} // end of main function
Could you be a little more specific? I know I have them initialized and they are supposed to show the user input max and min numbers. But where would I do that before the loop? Because they take on different values while in the loop?
Actually that was not very good advice, I copied your code and compiled it with that change and it seemed to work, that's why I suggested it. The real problem is that if your first number is the minimum, then it will not be recorded. You get the first value from user input before the loop, then in the loop it checks to see if it is the maximum, then requests a new number, then checks to see if it is the minimum. The first number entered never gets checked to see if it is the minimum.