1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <string> // for string class
#include <iostream> // for cin and cout
using namespace std;
int initArrays ( int m, string name [], double age [], string state [] )
{
int count = 0;
cout << "Enter the name, age and state of each family member on a single line in the following format: Name Age State (two letter abbreviation). When finished type * to end: ";
while ( count < m )
{
cin >> name[count];
//if ( name[count].compare ( "*" ) == 0 ) break; // ************
if( name[count] == "*" ) break ;
cin >> age[count] >> state[count];
// state[count] = getTexas (state[count]);
count ++;
}
return count;
}
void printAll ( string name [], double age [], string state [], int m )
{
printf ( "\n%-20s%10s%8s\n",
"Family Member Name", "Age", "State" );
for ( int i = 0; i < m; i++ )
printf ( "%-20s%11.2f%5s\n", name[i].c_str(), age[i], state[i].c_str() );
}
double getAverage ( double age [], int m )
{
if ( m <= 0 ) return 0; // make sure we don't divide by 0
double sum = 0;
for ( int i = 0; i < m; i++ )
sum = sum + age[i];
return sum / m;
} // end getAverage
//********************************
//void getTexas ( string name [], string state [], string tx [], int i )
// The function getTexas is supposed to identify all of the names that are in TX and then write them out.
void printTexas ( const string name [], const string state [], int m )
{
puts( "names of residents of Texas\n-------------------- " ) ;
for( int i = 0 ; i < m ; ++i )
if ( state [i] == "tx" || state [i] == "TX" ) puts( name[i].c_str() ) ;
puts( "----------------------------" ) ;
}
//********************************
int main ()
{
const int max = 50; // needs to be a constant to use in array declarations
int used; // the number of student entries actually active
string name [max];
double age [max];
string state [max];
used = initArrays ( max, name, age, state );
printAll ( name, age, state, used );
// getTexas ( name, state, tx );
printTexas( name, state, used ) ;
cout << "The family's average age is: " << getAverage ( age, used ) << endl;
cout << "Bye" << endl;
// system ("Pause"); // Visual C++
} // end main
|