如何捕获字符串数组的错误
How to error trap a string array
在提示用户做出选择并输入3之后,他们被要求从给定的列表中输入一个名字。如果他们输入的名字不在列表中,程序需要输出一条语句,说明输入的名字不在列表中
我已经尝试了我能想到的所有方法,如果我删除错误捕获,程序可以正常工作。
问题在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;
}
变量Loc
是在SpecTime
的秒行中创建的,而不是在循环中使用的变量Loc
。看一下
for (int Loc = 0; Loc < Size; Loc++)
int Loc = 0
意味着您创建了一个新变量,它遮蔽了原来的变量。将其更改为Loc = 0
,代码应该可以正常工作。
关于变量隐藏的更多信息:在c++中,何时可以在同一作用域中看到两个同名的变量?
相关文章:
- "错误:数组下标的无效类型'float [10001][float]'是什么意思?
- 错误:数组下标的类型"const bool[int]"无效
- Qt并发错误:数组初始化需要大括号括起来的初始值设定项列表
- 使用GCC 4.8构建错误:数组用作初始化器
- 如何修复问题中的"teminated due to timeout"错误 数组操作 |黑客兰克?
- | 9 |错误:非静态数据成员'matrix :: row'| 9 |错误:数组绑定不是在']']'令牌|之前的整数常数
- 错误:数组的总大小不得超过0x7fffffff字节
- 错误:无法将参数“1”的“double*”转换为“double”,错误:数组下标的类型“double[double]”无
- 返回并强制转换数组指针(错误:数组下标的类型..无效)
- 返回c++中的字符错误数组
- VC++错误:数组实例化的大小来自模板化的静态常量
- c++mex错误:数组下标的类型“double[mwSize]”无效
- 错误数组用作初始值设定项,我不知道错误
- 编译错误 - 数组下标'char[int]'的类型无效
- 向量在我的类中.错误:数组new [-fpermissive]中的圆括号初始化项
- 错误:数组下标的类型无效
- 错误:数组new中的size必须是整型[-fpermissive]
- 错误:数组下标的“Pixel*[__complex__ int]”类型无效
- c++错误数组显示
- C++:错误:数组下标的类型“size_t{aka long unsigned int}[size_t{aka long