Hello all, I'm getting an error telling me "'}' is expected at the end of input" but '}' is there. Any help with these issues? Here's what I have so far:

#include <iostream>
using namespace std ;

void displayTitle ()
{
     cout << "Active Duty Navy Personnel Program" << endl ;
}

int main ()
{

    displayTitle () ;
 
   
    int Age[50] ;    // age of personnel
    int I ;          // number of ages entered
    int sum ;        // sum of ages
    float Average ;  // average of ages entered
    sum = 0 ;
    Average = 0 ;
    for (I = 0 ; I < 50 ; I++)
    {
       cout << "Enter current age of AD Navy Personnel" << ( I + 1 ) << endl ;
       cin >> Age[I] ;
       
       if ( Age[I] >=18 ){
          sum = sum + Age[I] ;
       }
       else {
             I = I - 1 ; // the same element the Age array will be read
        }
    Average = sum/50 ;
    for ( I = 0 ; I < 50 ; I ++ )
    {
        cout << "Age of Personnel" << ( I + 1 ) << "is : "
        << Age[I] << endl ;
       }
       cout << "The current average age of AD Navy Personnel is : "
        << Average << endl ;
       
    cin.get () ;
    return (0) ; // terminate with success
   
}

You need a } right after line 31 in the above code to close off the first for loop. Then they should all balance out.

Also, unrelated to this:

for ( int i = 0 ; i<I; i++ )
    {
        cout << "Age of Personnel" << ( i + 1 ) << "is : "
        << Age[i] << endl ;
       }

Your second for loop should go from something to letter I because you've kept track of the number of ages over 18 in your array, otherwise you'll print junk for the remaining open slots until 50.

Edited 6 Years Ago by jonsca: n/a

Thx for replying....I'm confused because line 31 has '}' so what line should it be in? And I'm confused about what you mean for the second loop.

Thx for replying....I'm confused because line 31 has '}' so what line should it be in? And I'm confused about what you mean for the second loop.

Put one more bracket } right after that one on line 31, your first for loop never stops before you calculate average.

Your brace on line 31 closes off the else paired with the for above it. All of that code is enclosed in a for loop starting on line 21. Placing a } after line 31 but before Average = etc will close off the for loop from line 21. Since you were missing the closing brace, there were are odd number of braces and the last brace in your program (line 44) was being (mistakenly) paired with the one from the for loop on line 34.

As far as the second for loop goes, ask yourself why you are taking I= I-1 in the prior for loop. Because you don't include those records for servicepeople under 18. So if your list was 50 long and you're excluding say 5 people, you'll only have 45 ages. You want your second loop to know this because if you've only written in 45 names, the last 5 slots in the array will contain garbage that you won't want in your final printout.

end the for loop in line 25

That is incorrect. Notice the OP has statements requiring the use of the loop variable up until 31.

This article has been dead for over six months. Start a new discussion instead.