c++ - 如何错误捕获字符串数组

标签 c++ arrays function for-loop error-handling

提示用户进行选择并输入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,代码应该可以正常工作。

有关可变阴影的更多信息:In C++, when can two variables of the same name be visible in the same scope?

关于c++ - 如何错误捕获字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761094/

相关文章:

swift - 如何通过发送者以外的标签调用特定的UIButton?

c++ - 使用 atan2() 函数进行模拟时出现奇怪的 "yaw"行为

javascript - 如何在 javascripts 中按经度纬度距离对数组项进行排序?

ruby - 分割数组。 ruby

function - 如何在 Kotlin 中初始化嵌套数据类?

c++ - 找不到标识符 : __mul128

c++ - 专门的纯虚拟模板函数的另一个问题(未定义的引用)

c++ - 在 Linux 上用 C++ 编写类似 pexpect 的程序

c++ - 在迭代期间哪个更快的并发队列 <> 与互斥队列 <>

ruby - 在 Ruby 数组中收集重复项的最快/单行方法?