help with loop

I'm new to programming, but i feel pretty good about this program except i can't get it to loop. i would like it to allow the user to enter new numbers until they input 'N'. any help would be appreciated it says that i'm not using a loop but i don't understand whats missing. thank you so much in advance.
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
\*This program is designed to take five numbers input by the user and return the; sum, product, average,
largest value and smallest value of the provided numbers.

Algorithm steps:
1. Create a function to calculate the sum of 5 numbers
2. Create a function to calculate the product of 5 numbers
3. Create a function to calculate the average of 5 numbers
4. Create a function that determines the largest of 5 numbers
5. Create a function that determines the smallest of 5 numbers
6. Create a main function
7. Create an output prompting the user to 
*/

#include <iostream>
#include <string>
#include <cmath>
#include <cassert>
using namespace std;

double findSum(double num1, double num2, double num3, double num4, double num5) {

	double sum = (num1 + num2 + num3 + num4 + num5);
	return sum;
}

double findProduct(double num1, double num2, double num3, double num4, double num5) {

	double product = (num1 * num2 * num3 * num4 * num5);
	return product;
}

double findAvg(double num1, double num2, double num3, double num4, double num5) {

	double avg = findSum(num1, num2, num3, num4, num5)/5;
	return avg;
}

double findLargest(double num1, double num2, double num3, double num4, double num5) {
	if (num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5) {
		return num1;
	}
	if (num2 > num1 && num2 > num3 && num2 > num4 && num2 > num5) {
		return num2;
	}
	if (num3 > num2 && num3 > num1 && num3 > num4 && num3 > num5) {
		return num3;
	}
	if (num4 > num2 && num4 > num3 && num4 > num1 && num1 > num5) {
		return num4;
	}
	if (num5 > num2 && num5 > num3 && num5 > num4 && num5 > num1) {
		return num5;
	}
}

double findSmallest(double num1, double num2, double num3, double num4, double num5) {
	if (num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5) {
		return num1;
	}
	if (num2 < num1 && num2 < num3 && num2 < num4 && num2 < num5) {
		return num2;
	}
	if (num3 < num2 && num3 < num1 && num3 < num4 && num3 < num5) {
		return num3;
	}
	if (num4 < num2 && num4 < num3 && num4 < num1 && num1 < num5) {
		return num4;
	}
	if (num5 < num2 && num5 < num3 && num5 < num4 && num5 < num1) {
		return num5;
	}

}

int main()
{
	char newnumbers;
	double n1 = 0;
	double n2 = 0;
	double n3 = 0;
	double n4 = 0;
	double n5 = 0;
	//double sum = findSum(n1, n2, n3, n4, n5);
	//double num1, num2, num3, num4, num5; FIXME // Fixed 
	while(true)
	cout << "input 5 numbers separated by a space" << endl;
	cin >> n1 >> n2 >> n3 >> n4 >> n5;
	cout << "The sum of the 5 numbers is: " << findSum(n1, n2, n3, n4, n5) << endl;
	cout << "The product of the 5 numbers is: " << findProduct(n1, n2, n3, n4, n5) << endl;
	cout << "The average of the 5 numbers is: " << findAvg(n1, n2, n3, n4, n5) << endl;
	cout << "The largest of the 5 numbeers is: " << findLargest(n1, n2, n3, n4, n5) << endl;
	cout << "The smallest of the 5 numbeers is: " << findSmallest(n1, n2, n3, n4, n5) << endl;
	cout << "Would you like to enter new numbers?? (y = yes/ n = no)" << endl; //prompts the user to enter y (yes) or n (no)
	cin >> newnumbers;
	if (newnumbers == 'y') //if newnumbers == 'y' then allows user to enter 5 new numbers
		continue;
	else
		break;
	
	cin.ignore(1000, '\n');
	cin.get();
	return 0;
}
Here is your while loop:

1
2
while(true)
	cout << "input 5 numbers separated by a space" << endl;


There is nothing else inside your loop. If you want more statements in your loop, USE BRACES { and }
Hello TheJast,

I offer this version of "main" to show you what a few blank lines can do and along with proper indenting what the code really looks like:

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
int main()
{
	char newnumbers;
	double n1 = 0;
	double n2 = 0;
	double n3 = 0;
	double n4 = 0;
	double n5 = 0;
	//double sum = findSum(n1, n2, n3, n4, n5);
	//double num1, num2, num3, num4, num5; FIXME // Fixed 

	while (true)
		cout << "input 5 numbers separated by a space" << endl;

	cin >> n1 >> n2 >> n3 >> n4 >> n5;

	cout << "The sum of the 5 numbers is: " << findSum(n1, n2, n3, n4, n5) << endl;
	cout << "The product of the 5 numbers is: " << findProduct(n1, n2, n3, n4, n5) << endl;
	cout << "The average of the 5 numbers is: " << findAvg(n1, n2, n3, n4, n5) << endl;
	cout << "The largest of the 5 numbeers is: " << findLargest(n1, n2, n3, n4, n5) << endl;
	cout << "The smallest of the 5 numbeers is: " << findSmallest(n1, n2, n3, n4, n5) << endl;
	cout << "Would you like to enter new numbers?? (y = yes/ n = no)" << endl; //prompts the user to enter y (yes) or n (no)

	cin >> newnumbers;

	if (newnumbers == 'y') //if newnumbers == 'y' then allows user to enter 5 new numbers
		continue;
	else
		break;

	cin.ignore(1000, '\n');
	cin.get();

	return 0;
}


Looking at lines 12 and 13 you will see that line 13 is the only line that is connected with the while loop. What you are missing is the {}s to create a block. What your code should look like is:

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
int main()
{
	char newnumbers;
	double n1 = 0;
	double n2 = 0;
	double n3 = 0;
	double n4 = 0;
	double n5 = 0;
	//double sum = findSum(n1, n2, n3, n4, n5);
	//double num1, num2, num3, num4, num5; FIXME // Fixed 

	while (true)
	{
		cout << "input 5 numbers separated by a space" << endl;

		cin >> n1 >> n2 >> n3 >> n4 >> n5;

		cout << "The sum of the 5 numbers is: " << findSum(n1, n2, n3, n4, n5) << endl;
		cout << "The product of the 5 numbers is: " << findProduct(n1, n2, n3, n4, n5) << endl;
		cout << "The average of the 5 numbers is: " << findAvg(n1, n2, n3, n4, n5) << endl;
		cout << "The largest of the 5 numbeers is: " << findLargest(n1, n2, n3, n4, n5) << endl;
		cout << "The smallest of the 5 numbeers is: " << findSmallest(n1, n2, n3, n4, n5) << endl;

		cout << "Would you like to enter new numbers?? (y = yes/ n = no)" << endl; //prompts the user to enter y (yes) or n (no)

		cin >> newnumbers;

		if (newnumbers == 'y') //if newnumbers == 'y' then allows user to enter 5 new numbers
			continue;
		else
			break;
	}

	cin.ignore(1000, '\n');
	cin.get();

	return 0;
}

As you can see a few well placed blank lines breaks up the code and makes it easier to read and work with. This is not a problem because the compiler does not care about white space or blank lines.

In your original code the if statement has "continue" and "break", but there is no place to continue to or break out of. This should have caused you a compile error. When the if statement is put inside the block of the while loop it works fine because it now has someplace to continue to or break out of.

For future use I would say that this is the more used way of writing "cin.ignore".
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. .

I have not tested the code yet, so I am not sure how it works. I will get to that shortly and if I find anything else I will let you know.

Hope that helps,

Andy
Topic archived. No new replies allowed.