使用常量大小的声明符输出数组值

时间:2015-08-01 08:33:23

标签: c++ arrays function for-loop parallel-arrays

在我的程序中,4个函数中的3个将不会一直运行,它似乎与我的数组有关。

当它试图输出包含名称的数组时,它们似乎都失败了。我想知道我是否可以尝试不正确地输出数组。

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

double FastSkier(double [], string[], int);     //Function for finding fastest skier
double AvgTime(double[], int);                  //Function for finding Average
int SpecTime(double[], string[], int);          //Function for finding the time of the name entered
int SkiAndTime(double[], string[], int);        //Function to list all Skiers and their times

int main()
{
const int Size = 5; //size of arrays
//int fastest;  //variable for FastSkier function: fastest skier
//double Avg;       //Average
double time[Size] = { 2.03, 2.40, 1.85, 1.90, 2.50 };   //array for Skier times
string name[Size] = { "Leela", "Sarah", "Anna", "Keesha", "Heidi" };    //array for Skier names
//int Loc;      //assignment variable to for locations within arrays
int choice;

for (int count = 1;; count++)
{
    cout << "Enter 1 to find the fastest Skier" << endl;
    cout << "Enter 2 for the average time of the Skiers" << endl;
    cout << "Enter 3 to find the time of a specific Skier \n";
    cout << "Enter 4 to display all Skiers and their times \n";
    cout << "\n";
    cin >> choice;

    if (choice == 1)
        FastSkier(time, name, Size);
    else if (choice == 2)
        AvgTime(time, Size);
    else if (choice == 3)
        SpecTime(time, name, Size);
    else if (choice == 4)
        SkiAndTime(time, name, Size);
    else
        break;

}
system ("pause");
return 0;
}



double FastSkier(double time[], string name[], int Size)
{
int Loc;    //location of data within array, value determined by for-loop
double fastest=time[0]; //variable to find fastest time for Skier, initialized at first value of time
for (Loc = 1; Loc < Size; Loc++)    //cycles through all values of time comparing each one to find the lowest value
{
    if (time[Loc] < fastest)
        fastest = time[Loc];
}
cout << "The fastest Skier is " << name[Loc] << " with a time of " << time[Loc] << endl;
return 0;
}


double AvgTime(double time[], int Size)
{
int Loc;    //location of data within array, acts as a counter in this function 
double Avg; //Average
double sum = 0; //sum of all values within time[]

for (Loc = 0; Loc < Size; Loc++)
    sum += time[Loc];
Avg = sum / Size;

cout << "The average time for Skiers is " << fixed << setprecision(2) << Avg << endl;
cout << "\n";
cout << "\n";
return 0;
}


int SpecTime(double time[], string name[], int Size)
{
string Skier;
cout << "Skiers \n";
cout << "    " << name[Size] << endl;

cout << "Enter the name of the Skier to view their time \n";
cin >> Skier;

for (int Loc = 0; Loc < Size; Loc++)
{
    if (Skier == name[Loc])
    {
        cout << Skier << " has the time " << time[Loc] << endl;
        break;
    }
    /*else
        while (Skier != name[Loc])
        {
            cout << "The name you entered is not a current competitor in this competition \n";
            cout << "Please enter a name from the list \n";
            cin >> Skier;
        }*/
}
return 0;
}


int SkiAndTime(double time[], string name[], int Size)
{
cout << "Skiers             Times" << endl;
cout << name[Size] << "           " << time[Size] << endl;
return 0;
}

0 个答案:

没有答案
相关问题