Arrays

Having an issue with this code. I am having issues when a user enters the choice 3 to find the time of a skier. It only shows Heidi's name and does not let you enter the name without getting the "The name you entered is not a valid skier" message.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

double FastestSkier (double[], string[], int); // Fastest Skier Function
double AverageTime (double[], int); // Finding Average Time Function
int SpecificTime (double[], string[], int); // Finding Time based on Name Function
int NameAndTime (double[], string[], int); // Listing All Skiers and their Times

int main()
{
	const int size = 5.0; // 5 Arrays

	double time [size] = {2.03,2.40,1.85,1.90,2.50}; // Assigning Array Times
	string name [size] = {"Leela", "Sarah","Anna","Keesha", "Heidi"}; // Assigning Array Names

	int select;

	for (int count=1;; count++)
	{
		cout <<"Enter 1 to determine the fastest Skier ";
		cout <<endl;
		cout <<"Enter 2 to calculate the average time ";
		cout <<endl;
		cout <<"Enter 3 to find the time of a Skier ";                        // Menu for input
		cout <<endl;
		cout <<"Enter 4 to display the list of Skiers ";
		cout <<endl;
		cout <<"To exit enter any other number ";
		cout <<endl;
		cin >> select;
		                           // If input is greater than 4 program will terminate
		if (select > 4)
			  break;

		switch (select)
		{
		case 1:
			FastestSkier (time,name, size - 1);
			break;

		case 2:
			AverageTime (time,size);
			break;

		case 3:
			SpecificTime (time, name, size - 1);
			break;

		case 4:
			NameAndTime (time, name, size - 1);
			break;


		}

	}

	system ("pause");
	return 0;

}

	double FastestSkier (double time[], string Name[], int size)
	{
		int Loc; // location of data

		int index;
		double fastest = time[0]; // variable to find the fastest time for the skier
		for (Loc = 0; Loc <size; Loc++)
		{
			if (time[Loc] < fastest)
				fastest = time[Loc];
			index = Loc;
		}
		cout <<endl;
		cout << "The fastest skier is " <<Name [index - 1] << " with a time of " <<time [index-1] <<endl;  //Output Fastest Skier
		cout <<endl;
		return 0;
	}

		double AverageTime(double time[],int size)
		{
			int Loc;
			double Avg; // Average variable
			double sum = 0; // Sum of all variables within time []

			for (Loc = 0; Loc <size; Loc++)
				sum+=time[Loc];
			Avg=sum/size;

			cout <<"The average time is " <<fixed <<setprecision(3) <<Avg <<endl;
			cout <<endl;                                          // Output average Skier Time
			return 0;
		}

			int SpecificTime (double time[], string name[], int size)
			{
				string Skier;
				cout <<"Skiers";
				cout <<endl;
				cout << "     " <<name [size] <<endl;
				
				cout << " Enter the name of the skier to find their time: ";
				cin >> Skier;

				for (int Loc = 0; Loc < size; Loc++)
					if (Skier ==name[Loc])
					{
						cout <<Skier <<"'s" << " time is " <<time[Loc] <<endl;
						break;
					}
					else while (Skier != name[Loc])
					{
						cout <<"The name you entered is not a valid Skier. Please enter a name from the list";
						cout <<endl;
						cin >> Skier;
						break;
					}
					return 0;
			}
				int NameAndTime (double time[], string name[], int size)
				{
					cout <<fixed << setprecision(2) <<endl;
					cout <<"Skiers and their times" <<endl;
				cout << name [0] << "     " << time [0] <<endl;
				cout << name [1] << "     " << time [1] <<endl;
				cout << name [2] << "      " << time [2] <<endl;
				cout << name [3] << "    " << time [3] <<endl;
				cout << name [4] << "     " << time [4] <<endl;
				 return 0;
				}
Last edited on
First, make your code more readable, and use brackets for loops involving more than one line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
for (int Loc = 0; Loc < size; Loc++)
{

 if (Skier ==name[Loc])
 {
  cout <<Skier <<"'s" << " time is " <<time[Loc] <<endl;
  break;
 }
 else
 {

  while (Skier != name[Loc])
  {
   cout <<"The name you entered is not a valid Skier. Please enter a name from the list";
   cout <<endl;
   cin >> Skier;
   break;
  }

 }

}
 return 0;



The problem is that when you are searching for the name that was entered by the user, you aren't searching the entire array. All you are doing is incrementing the index and seeing if the next element of the array matches the name the user entered. You want to display a not found message only when the entire array has been searched not when any element doesn't match.

The function, SpecificTime() returns an int. Why when it always returns zero?
Last edited on
Topic archived. No new replies allowed.