Need help

How would I rearrange this to get constant variables. I need three of them. I am using this to calculate an average of integers. Are my variables initialized correctly?

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
#include <iostream>
using namespace std;

int main()
{
    int n, i;
    double num, sum=0.0, average;

    cout << "Enter a number of values from 2 to 10: ";
    cin >> n;

    while (n > 10 || n <= 2)
    {
        cout << "Invalid input!" << endl;
        cout << "Enter a number of values from 2 to 10: ";
        cin >> n;
    }

    for(i = 0; i < n; ++i)
    {
        cout << i + 1 << ". Enter number: ";
        cin >> num;
        sum += num;
    }

    average = sum / n;
    cout << "Average = " << average;

    return 0;
}
you just want 3 random constants?

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

#include <iostream>
using namespace std;

int main()
{
    const int one = 1;
    const int two = 2;
    const int ten = 10;

    int n{0}, i{0};
    double num{0.0}, sum{0.0}, average{0};

    cout << "Enter a number of values from 2 to 10: ";
    cin >> n;

    while (n > ten || n <= two)
    {
        cout << "Invalid input!" << endl;
        cout << "Enter a number of values from 2 to 10: ";
        cin >> n;
    }

    for(i = 0; i < n; ++i)
    {
        cout << i + one << ". Enter number: ";
        cin >> num;
        sum += num;
    }

    average = sum / n;
    cout << "Average = " << average;

    return 0;
}


initialize all variables. You don't have to init n, because you read it in later, for correctness but its better to initialize it anyway for debugging and consistency and best practices reasons. I did a few of them for you. If you don't need the value later, its also best to scope the loop variable to the for loop instead of making it persist longer.
Last edited on
Thank you for the help @jonnin. What you mean by scoping the loop variable. Are you talking about the i variable?
@akeilo,
Yes, that means that you do something like this:
1
2
3
4
5
6
7
// Not how "i" is defined and initialized inside the for loop
// as "size_t i = 0"

for (size_t i = 0; i < n; ++i)
{
	/* whatever */
}


It basically means you define it inside the for loop parentheses.

max
Last edited on
To check that the input is a valid number, consider:

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
#include <iostream>
#include <limits>
using namespace std;

int main()
{
	size_t n {};
	double num {}, sum {};

	while ((cout << "Enter a number of values from 2 to 10: ") && (!(cin >> n) || cin.peek() != '\n' || (n < 2 || n > 10))) {
		cout << "Invalid input!\n";
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}

	for (size_t i = 0; i < n; ++i) {
		cin.ignore(numeric_limits<streamsize>::max(), '\n');

		while ((cout << i + 1 << ". Enter number: ") && (!(cin >> num) || cin.peek() != '\n')) {
			cout << "Not a number\n";
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
		}

		sum += num;
	}

	cout << "Average = " << sum / n << '\n';
}

I interpreted this as using symbolic constants in the place of numeric literals in at least three ways. This would make changing the bounds of the problem later less error-prone.
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
#include <iostream>
using namespace std;
int main()
{
    // CONSTANTS
    const int LO = 2;
    const int HI = 10;
    const int NUMBERING_OFFSET = 1;

    int numVals;
    cout << "Enter a number of values from " << LO << " to " << HI << ": ";
    cin >> numVals;
    while (numVals > HI || numVals <= LO)
    {
        cout << "Invalid input!" << endl;
        cout << "Enter a number of values from " << LO << " to " << HI << ": ";
        cin >> numVals;
    }

    double input, sum = 0.0, average;
    for (int i = 0; i < numVals; ++i)
    {
        cout << i + NUMBERING_OFFSET << ". Enter number: ";
        cin >> input;
        sum += input;
    }

    average = sum / numVals;
    cout << "Average = " << average;
    return 0;
}
Topic archived. No new replies allowed.