C++ 如何捕获字符串数组的错误
提示用户进行选择并输入3后,将要求用户从给定列表中输入名称。 如果输入的名称不在列表中,程序需要输出一条语句,说明输入的名称不在列表中 我已经尝试了我所能想到的一切,如果我删除错误陷阱,程序运行良好 问题在C++ 如何捕获字符串数组的错误,c++,arrays,function,for-loop,error-handling,C++,Arrays,Function,For Loop,Error Handling,提示用户进行选择并输入3后,将要求用户从给定列表中输入名称。 如果输入的名称不在列表中,程序需要输出一条语句,说明输入的名称不在列表中 我已经尝试了我所能想到的一切,如果我删除错误陷阱,程序运行良好 问题在int-SpecTime函数中 #include <iostream> #include <string> #include <iomanip> using namespace std; double FastSkier(double [], strin
int-SpecTime
函数中
#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
string name[Size] = { "Leela" , "Sarah" , "Anna" , "Keesha" , "Heidi" }; //array for Skier names
double time[Size] = { 2.03 , 2.40 , 1.85 , 1.90 , 2.50 }; //array for Skier times
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 << "Enter any other number to end the program \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
return 0;
}
system ("pause");
return 0;
}
double FastSkier(double time[], string name[], int Size)
{
int Loc; //location of data within array, value determined by for-loop
int count; //Counter
double fastest=time[0]; //variable to find fastest time for Skier, initialized at first value of time
for (count = 1; count < Size; count++) //cycles through all values of time comparing each one to find the lowest value
{
if (time[count] < fastest)
Loc = count-1; //subtract 1 from count to adjust for array index
}
cout << "\n";
cout << "The fastest Skier is " << name[Loc] << " with a time of " << fixed << setprecision(2) << time[Loc] << endl;
cout << "\n";
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 << "\n";
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; //Name of Skier entered by user
int Loc=0;
bool List = true;
cout << "Skiers \n";
for (int Loc = 0; Loc < Size; Loc++) //For-loop used to output and display all names of Skiers
{
cout << " " << name[Loc] << endl;
}
cout << "Enter the name of the Skier to view their time \n";
cin >> Skier;
for (int Loc = 0; Loc < Size; Loc++) //For-loop used to find the desired Skier's time
{
if (Skier == name[Loc])
{
cout << Skier << " has the time " << fixed << setprecision(2) << time[Loc] << endl;
cout << "\n";
break;
}
}
if (Skier != name[Loc]) //error trap for inputted names that are not listed
{
cout << "The name you entered is not a current competitor in this competition \n";
cout << "\n";
//break;
}
return 0;
}
int SkiAndTime(double time[], string name[], int Size)
{
cout << "Skiers Times" << endl;
cout << "\n";
for (int All = 0; All< Size; All++)
cout << name[All] << " " << fixed << setprecision(2) << time[All] << endl;
cout << "\n";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
双人快速滑雪者(双人[],字符串[],整数)//寻找最快滑雪者的功能
双平均时间(双[],整数)//求平均值的函数
int SpecTime(双精度[],字符串[],int)//用于查找输入名称的时间的函数
int SkiAndTime(双精度[],字符串[],int)//函数列出所有滑雪者及其时间
int main()
{
const int Size=5;//数组的大小
字符串名称[大小]={“Leela”、“Sarah”、“Anna”、“Keesha”、“Heidi”};//滑雪者名称数组
双倍时间[Size]={2.03,2.40,1.85,1.90,2.50};//滑雪者时间数组
智力选择;
对于(int count=1;count++)
{
cout变量Loc
,它是在specime
的秒行中创建的,不是循环中使用的变量Loc
。
看看这条线
for (int Loc = 0; Loc < Size; Loc++)
for(int-Loc=0;Loc
int Loc=0
意味着您创建了一个新变量,该变量将隐藏原始变量。将其更改为Loc=0
,代码应能正常工作
关于变量隐藏的更多信息:您的问题到底是什么?您不知道如何检查名称是否在列表中吗?不,我不知道如何检查。我尝试的所有操作都不会触发错误消息,或者即使输入了正确的输入也会触发消息准备下一次的最短代码示例,并详细描述您的问题下一次。你会更快地得到答案。阅读和阅读。