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
   
}

Dani AI

Generated

The compiler message means the parser reached the end of the file while a block was still open. That usually comes from an unclosed control block (for/if/else) or braces that don't line up. Reformat the file and use your editor's brace-matching to spot the open scope. That will also reveal why statements like the average calculation and return are being treated as part of the input loop instead of after it.

Beyond the brace problem, there are a few logic and robustness issues to fix:

  • Don't change the loop index from inside the loop (e.g., I = I - 1). Use a separate count (or push into a container) for the number of valid entries. Modifying the loop control variable makes code hard to reason about and can cause infinite loops.
  • Only divide by the actual number of valid entries and use a floating-point cast to avoid integer division: average = static_cast<double>(sum) / count; and guard against count == 0.
  • Validate input (non-numeric input, out-of-range ages) and clear/ignore the input buffer when needed.

A compact pattern that avoids the original pitfalls (uses a container, counts valid entries, and keeps input and output separate):

#include <iostream>
#include <vector>
#include <limits>

const int MAX = 50;
std::vector<int> ages;
ages.reserve(MAX);
int age;
while (ages.size() < MAX && (std::cin >> age)) {
    if (!std::cin) { 
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        continue;
    }
    if (age >= 18) ages.push_back(age);
}
if (!ages.empty()) {
    int sum = 0;
    for (int a : ages) sum += a;
    double avg = static_cast<double>(sum) / ages.size();
    // print ages and avg
}

This builds on suggestions from and : fix the unmatched scope, stop mutating the loop variable, and calculate averages from the real count. Also consider enabling compiler warnings and using an editor/IDE that highlights brace pairs to prevent similar errors.

Recommended Answers

All 6 Replies

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.