Need help with for loop!

The user will enter the value for "n" where "n" is the number of values in the data set to be entered. Give the user 3 attempts at entering a value for " n " where "n" is in the range from 0 to 20, inclusive. If the number "n" entered is out of range, write an error message and continue. If the user does not enter a valid value for "n" within the 3 attempts print an error message and terminate the program.

Include the rounding function to round the Average value to 3 decimal places.
The n values entered for input can be positive, negative, or zero. But what about the n value? Use data validation to validate n.

I put the directions in here only so you know what I am ultimately after.

My problem is that I am supposed to be setting up a for loop within the program after my do-while loop. I am not sure how to set up a for loop.

This is what I have so far.

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
int main()
{
     double n;                       // input - 1st value entered by user
     int count;                      // 
     double averageNumber;           // output - average, with 3 decimal        
     double largeNumber;             // output - largest value of the 3 entered
     double smallNumber;             // output - smallest value of the 3 entered
     
          
     instruct();
     
     //Enter 3 values for n within a range of 0 - 20.
     count = 0;
     do 
{ 
     count + 1;
     cout << "Enter a value for n within a range of 0 - 20: "; cin >> n;
     cout << endl << endl;
}    while (!(n >= 0 && n <= 20) && count < 3);
     
     if (n >= 0 && n <= 20);
     
     else
     
     if (n == 0);
        cout << " The number is valid but cant get data set from 0" << endl;
     
     
     
     //Determine which value entered is highest, lowest, and the average of the 3
    
line 16 count + 1; doesn't do anyhting
it should be:
count += 1;

which refers to :
count = count + 1;

OR even shorter:
++count
Last edited on
I appreciate the catch on that....is there anyone able to help with the FOR loop?
A for loop is a short hand tool; It's a while loop in disguise.

With a while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//pseudo code
{
 var = start value
 while( condition ) {
 	loop body
 	do something //a single statement, i.e. one line ending with a ;
 }
} //var is out of scope here

//--------------------------------------------------------------------------------
//example in C++
{
 int i = 1;
 while( i <= 10 ) {
 	//loop body
 	double num;
 	cout << "Enter a number: ";
 	cin >> num;
 	cout << i << ". " << num;

 	//increment i so it will be greater then 10 after 10 iterations
 	i++;
 }
}


With a for loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//pseudo code
for( var = start value; condition; do something ) {
	loop body
} //var is out of scope here

//--------------------------------------------------------------------------------
//example in C++
for( int i = 1; i <= 10; i++ ) {
	//loop body
	double num;
	cout << "Enter a number: ";
	cin >> num;
	cout << i << ". " << num;
}
Last edited on
Topic archived. No new replies allowed.