using if statement to print the largest/smallest values

Write your question here.
so, this is what i have. i wanted to include it all because i don't know what people need to see in order to tell if i have put things in the right place, but the only thing that i need help with is how to get an output that states the largest value and the smallest value of the external input.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 int main()
{
	int counter = 0;				// counter for total number of input values
	double user_input;				// variable that holds the current input value
	double average = 0.0;			// variable that holds the average calculated
	double sum_of_user_input = 0.0;	// variable that holds the sum of the values inputted
	double largest_value = 0.0;		// variable that holds the largest value inputted
	double smallest_value = 0.0;	// variable that holds the smallest value inputted
	bool more_entry = true;			/* a flag to indicate whether the just input value is to be
									* processed as one of the input values or if it is just a
									* value to indicate NO MORE. it begins under the assumption
									* that we have more input values to come */

	cout << "Please enter a non-negative number! (Entering a number < 0 means you want to end input): \n";
	cin >> user_input;

	if (user_input < 0)				// if the input is less than zero
	{
		more_entry = false;			// then 'turn the flag' and avoid entering the loop
	}

	while (more_entry == true)			// while this new input is a valid input value
	{
		counter = counter + 1;			// incrementing the count of the non-negative numbers
		sum_of_user_input = sum_of_user_input + user_input;	// keeping a running total
		cout << endl;
		cout << "Please enter a non-negative number (< 0 means you are finished):  \n";
		cin >> user_input;
		if (user_input < 0)
		{
			more_entry = false;			// no more input values from user
		}
	}

	cout << endl;

	if (counter > 1)
	{
		average = sum_of_user_input / counter;		// defining the average
		cout << "Total number of valid input values accepted: " << counter << endl;
		cout << "The average of all input values is: " << average << endl;
		cout << "The sum of all input values is: "<< sum_of_user_input << endl;

		if (largest_value <= user_input)
		{
			largest_value = user_input;			// if the user input is >= current largest number, then it takes that value
			cout << "The largest value inputted is: " << largest_value << endl;
		}
		if (smallest_value >= user_input)
		{
			smallest_value = user_input;
			cout << "The smallest value inputted is: " << smallest_value << endl;
		}


	}								// this ends if counter > 1

	else							// counter is <= 1
	{
		if (counter == 1)
		{
			cout << "Since you only inputed one non-negative number, the ";
			cout << "average is the input value: " << sum_of_user_input << ".\n";
		}							// this ends if counter is equal to 1

		else						//counter is < 1
		{
			cout << "You have not inputted any value. ";
			cout << "I cannot calculate the average." << endl;
		}							// this ends else counter < 1
	}								// this ends else counter <= 1

	cout << "Bye!" << endl;
	return 0;
	system("PAUSE");

}


i have been working on this for hours today and cannot seem to figure it out...any help would be hugely appreciated!
Last edited on
this is the part that i am focused on. i am assuming it is in the right place and that my logic is just flawed...i can't seem to figure this out...

1
2
3
4
5
6
7
8
9
10
                if (largest_value <= user_input)
		{
			largest_value = user_input;			// if the user input is >= current largest number, then it takes that value
			cout << "The largest value inputted is: " << largest_value << endl;
		}
		if (smallest_value >= user_input)
		{
			smallest_value = user_input;
			cout << "The smallest value inputted is: " << smallest_value << endl;
		}
Last edited on
I don't know what exactly your problem is?

Maybe that the output occurs only when the user input is the largest value? If so take the cout out of the if block
I think you need to add this piece of code in the while loop where you are finding the sum.
Topic archived. No new replies allowed.