Hello all, I am getting a few errors with my code, I have been playing around with it but I guess I still needed a few tweaks. The errors and code are as follows:

23 C:\myC++projects\averageproj.cpp expected `;' before '}' token
27 C:\myC++projects\averageproj.cpp expected primary-expression before '<<' token
34 C:\myC++projects\averageproj.cpp expected `}' at end of input

#include <iostream>

using namespace std ;

int main ()
{
    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 = 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 ;
            return (0); // terminate with success
            
            }

You commented out your semicolon on that line. Also, I'm not sure if your file was truncated but you don't have a closing brace for main().

EDIT: Also your inner for loop is going to end before your outer one finishes calculating the average. Move that final calculation Average = sum/50; outside of the loop. I would change the loop variable on your inner for loop to something else, everything is going to get muddled with your changes to I. So just move the inner for loop to the outside of the other one.

EDIT2: You will need to cast your sum value to make your average work. Also you can move your final output of the average age outside of both loops. Summary: for loop (then close it off) | calculate average | for loop (then close it off) | display average

Apologies for the edits, I'm not quite awake yet lol

Edited 6 Years Ago by jonsca: Just cause

Thx for the reply but I'm very new to this and without examples, I really don't understand what you mean.

I = 1 // the same element the Age array will be read ; You need the semicolon after 1 which you put at the end of your comment. Anything after the comment (//) is ignored. Start with that.

Then, you need to change I = 1; to I--; anyway, since with you setting I = 1 you are moving back to the beginning of the array, not just by one step.

As far as the cast goes, you are dividing two integers. If you divided 7/2 the result is 3, it truncates. If you divide anything with a quotient less than 1, it goes to zero, e.g., 3/7 = 0, even though you have a float to receive the division, it will still produce 0. So casting says that if you put Average = ((float) sum)/50; (parens could be around either numerator or denominator or both) you will get the proper answer.

Edited 6 Years Ago by jonsca: n/a

Thanks again but I'm still not getting it because the loops look closed to me. Sorry for being slow but I'm trying to teach myself and alot of things still don't make sense.

No, you're not slow, I'm just getting too far ahead of myself and trying to help you fix everything at once, apologies.

declare I out here so we can keep track of it
for loop #1 (over 0 to 50)
{
     keep a running total of sum (which you are doing just fine
     if (over 18)
           I--;  (bring it back by 1 position (we want to know how many we stepped back in the end so we can use it on the next loop)

}
Average = calculation;
for(make it small i=0 to I)
{
      output age[i] which you are doing just fine

}
output average and message

Edited 6 Years Ago by jonsca: n/a

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