outputting certain array value
How would you go about menu option 3 which is to find the time of a skier? What would a code example be?
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
//07/22/17
//Skier program
//Program shows skier names and their times.
#include <iostream>
using namespace std;
#include <iomanip>
void menu();
void displaymax(double); //display max protoype
int main() {
//*****************************************************************************
// Array Times
//*****************************************************************************
string names[] = { "Leela ", "Sarah ", "Anna ", "Keesha ", "Heidi " }; //
double times[] = { 2.03, 2.40, 1.85, 1.90, 2.50 };
menu(); //menu function call
int choice; //choice variable initialized
cin >> choice; // input choice
//CHOICE 1 If
if (choice == 1)
{
size_t idx = 0;
for (int i = 1; i < 5; i++)
{ //for loop choice 1
if (times[i] < times[idx])
{ //finding max
idx = i;
}
}
cout << "Fastest time is " << times[idx] << " by " << names[idx] << '\n'; //output fastest time
}
//CHOICE 2 if
else if (choice == 2)
{
double sum = 0; //initialize accumulator to 0
for (int i = 0; i < 5; i++) //find sum of numbers
sum += times[i];
cout << "The average time is " << sum/ 5 << endl; //output average
}
//CHOICE 3 if //??FIND THE TIME OF A SWIMMER??
else if(choice == 3) //??
{
}
//Choice 4 list swimmers
else if (choice == 4) //output list of swimmers
{
for (int count = 0; count < 5; count++) //
{
cout << names[count] << fixed << setprecision(2) << times[count] << endl; //
}
}
//choice 5 exit
else if(choice ==0)
{
exit(0); //exit call
}
return 0;
}
//***************************************************************************
// MENU FUNCTION
//***************************************************************************
void menu()
{
cout << "Enter 1 to determine the fastest skier. " << endl;
cout << "Enter 2 to calculate the average time" << endl;
cout << "Enter 3 to find the time of a skier" << endl;
cout << "Enter 4 to list all skiers and times." << endl;
cout << "0 to exit";
}
//****************
|
Since you know that the string array contain 5 names and the double array contains times corresponding to each of the names, you can just a for loop.
1 2 3 4 5 6 7
|
string name;
cin >> name;
for(int i = 0; i < 5; i++){
if(name == names[i]){
// fill this in yourself
}
}
|
Last edited on
Topic archived. No new replies allowed.