Hello guys....I appreciate any and all replies I get but I really needed to be shown how to incorporate a program title and a function call into this code and have it properly execute. I am really lost with programming so I don't usually understand explanations unless I can actually see an example. It's just my learning style, please bear with me. Thanks in advance for your understanding. :)

#include <iostream>
using namespace std ;

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

int main ()
{

    displayTitle () ;
    cin.get () ;
    return 0 ;
}

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

Dani AI

Generated

As pointed out, displayTitle() is already a function call. The next step is to split the work into small, named functions so main just orchestrates the program. In the posted code some of the age-processing appears after an early return/closing brace, which makes that block unreachable — move input and processing inside main or into functions. Also avoid manually changing loop indices to reject bad input; use an input loop that only accepts valid values.

A simple, safer structure:

#include <iostream>
#include <vector>
#include <numeric>

void showHeader() { std::cout << "Navy personnel age tool\n"; }

std::vector<int> collectAges(std::size_t maxCount) {
    std::vector<int> ages;
    while (ages.size() < maxCount) {
        std::cout << "Enter age #" << ages.size() + 1 << ": ";
        int a;
        if (!(std::cin >> a)) { std::cin.clear(); std::cin.ignore(10000,'\n'); std::cout << "Invalid input\n"; continue; }
        if (a < 18) { std::cout << "Must be 18 or older\n"; continue; }
        ages.push_back(a);
    }
    return ages;
}

double computeAverage(const std::vector<int>& v) {
    if (v.empty()) return 0.0;
    int sum = std::accumulate(v.begin(), v.end(), 0);
    return static_cast<double>(sum) / v.size();
}

int main() {
    showHeader();
    auto ages = collectAges(50);
    std::cout << "Average age: " << computeAverage(ages) << '\n';
    return 0;
}

Key troubleshooting tips:

  • Compute the average only after collecting all valid entries. Casting to double avoids integer-division truncation.
  • Handle non-numeric input with cin.clear() and cin.ignore().
  • Prefer std::vector and std::accumulate over raw arrays for safety and clarity (see std::vector and std::accumulate).
  • Keep return at the end of main and do not place needed code after it.

This keeps main readable, meets the “at least one function call” requirement beyond the title, and fixes common input/average bugs.

Recommended Answers

All 6 Replies

Sooooooo...... do you need help with anything......?

#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 ;
}

Now wasn't that cute....Actually, I'm in better shape than I was thx to firstPerson!

Appreciate your assistance! I also need to incorporate at least one function call within my code and that has me completely lost....Any help?

Appreciate your assistance! I also need to incorporate at least one function call within my code and that has me completely lost....Any help?

You already did, the displayTitle(); is a function call.

You also might want to make a function that returns the average, given an array and its size.

Thanks again!!

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.