В моей программе 3 из 4 функций не будут работать до конца, и, похоже, это как-то связано с моими массивами.
Кажется, что все они терпят неудачу, когда он пытается поместить массив, содержащий имена. Мне было интересно, если бы я мог как-то пытаться вывести массив неправильно.
#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;
}
Задача ещё не решена.