Hi, I'm trying to incorporate a program title and welcome message into my code and I'm not sure if I have done it correctly. I would also like to know how to incorporate the following function calls into my code:


Call Personnel Age
Call Calculate Average Age
Call Display Average Age

#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
    "Active Duty Navy personnel Program"
    "This program will find the average age of fifty AD Navy Personnel"
    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
}

Thanks in advance!!!

Functions must have a declaration and a definition. If the fucntion is defined before main() then the declaration and the definition are the same. If the function is declared before main() it is frequently called a prototype. To use a function within the program it must then be called.

The only funciton you have in your program as posted is main(). Lines 11 and 12 are technically worthless at the moment and may well cause an error message.

Assuming line 11 is the title of the program and you wish to display it using a function it could be done something like this:

#include <iostream>
using namespace std;

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

int main()
{
    displayTitle();

    cin.get();
    return 0;
}

Then to display line 12 using a funciton you might do something like this:

#include <iostream>
#include <string>
using namespace std;

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

int main()
{
    string greeting = "This program will find the average age of fifty AD Navy Personnel";
    displayTitle();   
    displayGreeting(greeting);
    cin.get();
    return 0;
}

void displayGreeting(string g)
{
    cout << g << endl;
}

The rest of your program can be divided into functions as desired. Just follow the two example syntaxes at your discretion. Remember, if a function needs some information from the function that called it, be sure to pass that information in the argument list to be placed in the () used in the function call as in the example of displayGreeting().

Thx but I must have done that really wrong because I now have alot of errors that I didn't have before. I'm not experienced enough to know how else to change this code. Here's what I did:

#include <iostream>
#include <string>
using namespace std ;

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

int main ()
{
    string greeting = "This program will find the average age of fifty AD Navy Personnel" ;
    displayTitle () ;
    displayGreeting (greeting) ;
    cin.get () ;
    return 0 ;
}

void displayGreeting (string g)
{
     cout << g << endl ;
}

    
    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
}

Also, I was trying to turn these statements into a call functions in my code:

Call Personnel Age
Call Calculate Average Age
Call Display Average Age

comment out lines 26 through 53, recompile and rerun. The } on line 18 ends main().

My post was an effort to use your code as the basis for an example of writing and calling functions. I limited the code to just that which was required to do the task. You should always work on one function (or action) at a time, and frequently just several lines within a function/action at a time. Never try to write a whole program or multiple functions/actions at once and try to debug. It is just too much of a hassle.

Once you can compile the two programs I gave you then I would try writing a getAges() function. First I would declare an array of type int called Ages and an int called I in main(). Ages should be declared with size 50 and I should be initialized with a value of zero. I would declare getAges() to be type void and would pass it two arguments, an array of type int called Ages and a reference to an int called I. I should be passed by reference to getAges() so the value of I determined in the getAges() will be retained back in main() and can then be passed to other functions later. Arrays are passed to functions by reference automatically, but they are the only type that I am aware of that is passed as a reference automatically.

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.