0

After the user is prompted to make a selection and they enter 3 they are then asked to enter a name from a list given. If they enter a name that is not on the list the program needs to output a statement saying that the name entered is not on the list

The program ends up breaking on Line 105 when an element that is not inside the array is entered.

I have tried everything I could think of and the program works fine if I remove the error trapping.

The issue is inside the int SpecTime function

#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++)   //For-loop used to find the desired Skier's time
{
    cout << Loc << " beginning of loop" << endl;
    if (Skier == name[Loc])
    {
        cout << Loc << " in correct" << endl;
        cout << Skier << " has the time " << fixed << setprecision(2) << time[Loc] << endl;
        cout << "\n";
        break;
    }
    if(Loc = Size)
    {
        cout << Loc << " in error" << endl;
        cout << "The name you entered is not a current competitor in this competition \n";
        cout << "\n";
        break;
    }
    cout << Loc << " end of loop" << endl;
}
/*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;
}
6
  • if you need more information or if I can clear anything up please let me know Commented Aug 1, 2015 at 12:37
  • 2
    What did you observe when stepping through your code with the debugger? Commented Aug 1, 2015 at 12:38
  • when it got to the second if statement on line 112 somehow Loc gets set to 5 which triggers the error message Commented Aug 1, 2015 at 12:40
  • Carrefully check that line. Programming is not magic, if Loc get set to 5 it is because you assign 5 to it, on that specific line. Commented Aug 1, 2015 at 12:51
  • I changed = to == but now the program breaks Commented Aug 1, 2015 at 12:54

2 Answers 2

3

One error is this:

 if(Loc = Size)

This should be:

 if(Loc == Size)

The second error is this:

 if (Skier == name[Loc])

On the last iteration of the loop, Loc goes beyond the bounds of your array. Since you are checking this condition first, the value of Loc has already gone past the last entry.

Also, why are you writing a loop for this that has no stop condition defined in the for? It should be simply:

for (int Loc = 0; Loc < Size; Loc++) 

or this:

bool found = false;
for (int Loc = 0; Loc < Size && !found; Loc++) 

Then the entire loop is much simpler:

bool found = false;
for (int Loc = 0; Loc < Size && !found; Loc++)   //For-loop used to find the desired Skier's time
{
    cout << Loc << " beginning of loop" << endl;

    if (Skier == name[Loc])
    {
        cout << Loc << " in correct" << endl;
        cout << Skier << " has the time " << fixed << setprecision(2) << time[Loc] << endl;
        cout << "\n";
        found = true;
    }
}

if ( !found )
{
    cout << Loc << " in error" << endl;
    cout << "The name you entered is not a current competitor in this competition \n";
    cout << "\n";
}

Notice that there is no longer a break statement in the loop. The reason is that the found flag is set to true, and the loop condition includes a check for this flag.


In reality, there is no need for this loop at all. The std::find function takes care of this:

#include <algorithm>
//...
string* ptrSkier = std::find(name, name + Size, Skier);
if ( ptrSkier == &name[Size] )
{
   // not found
}
else
{
   // found the name
   // the ptrSkier points to the skier found

   // to get the position of the found name:
   size_t Loc = std::distance(name, ptrSkier);

   // rest of code here using Loc
   //...      
}
Sign up to request clarification or add additional context in comments.

Comments

1

First: what PaulMcKenzie said.

Second: Your check for a valid competitor is incorrect. The fact that a single entry doesn't match the given name doesn't mean the name isn't somewhere else in the list.

You need to search the entire list for the name given, and only then decide if it's there or not. Start with a boolean variable, name_found, initially set to false. Then iterate through all names, and mark it true if you find the right one. Only when you get to the end of looping, and find that name_found is still false, can you conclude that the given name isn't there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.