using a for loop within a for loop problems

I have a for loop and I need to enter in sales for four quarters for each of the six divisions. The first example gives me my desired output.The second example which is a loop within a loop says the total sales are $-55535777000 etc.I would like to end with the second example of code to get the desired output. Let me know what you think, thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for(int count=0; count < 6; count++) 
{   cout <<"\nDivision    #" << (count + 1) << endl;

	
	cout << "Enter the amount for the first quarter" << endl; 
        cin >> quarter[0]; 
   
        cout << "Enter the amount for the second quarter" << endl; 
        cin >> quarter[1]; 
  
        
        cout << "Enter the amount for the third quarter" << endl; 
        cin >> quarter[2]; 
  
        
        cout << "Enter the amount for the fourth quarter" << endl; 
        cin >> quarter[3]; 

        div[count].Q(quarter[0], quarter[1], quarter[2], quarter[3]); 
               
}
Here is the second example
1
2
3
4
5
6
7
8
9
10
11
12
for(int count=0; count < 6; count++) 
{   cout <<"\nDivision    #" << (count + 1) << endl;
	   
    for(int count = 0; count < 4; count++)
    {   cout << "Quarter: " << (count + 1) << endl;
        cin >> quarter[count];

     div[count].Q(quarter[0], quarter[1], quarter[2], quarter[3]); 
               
    }
		
} 
You have to move line 8 out of the inner loop, after each 'quarter' element got the value from cin.
Hey thanks, it works. I was doing it before ,but when I would try to compile , it kept giving me some some kind warning referencing the count. I went ahead anyways and now the warning no longer comes. I put line 8 in the outerloop.Problem solved.
The warning should be about the fact that you are using the same name (count) for two variables but in your case is not a problem because they have different scopes
Last edited on
You should stick to standard convention of using i for the outter-most loop and then incrementing as you go in.

e.g
1
2
3
4
5
6
for (i = 0; etc) {
  for (j = 0; etc) {
    for (k = 0; etc) {
    }  
  }
}
Topic archived. No new replies allowed.