assistance with array function

Hello, i'm working on an assignment. I'm not done but i'm stuck on the search function and display that data. Could someone please point me in the right direction? When i search a player i want to limit the data of only that one player, so i'm having trouble assigning "i" to the array that was searched. thanks in advance.

Write a program that declares a struct to store the data of a football player (player's name, player's position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of a specific player, and update the data of a player. (You may assume that input data is stored in a file.) Before the program terminates, give the user the option to save data in a file. Your program should be menu driven, giving the user various choices

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
135
136

#include <iostream>
#include <string>
#include <fstream>					
#include <iomanip>


using namespace std;

struct players
{
	char name[20], position[50];
	int touchdowns, catches, passingYards,
		receivingYards, runningYards;


};
void printmenu();
void printData(struct players s[], int size);
void searchNamestr(struct players s[], int size, string searchName);

int main()
{
	int i,choice, notquit = 1;
	ifstream infile;
	string searchName;
	bool found = false;
	int index;

	infile.open("footballPlayers.txt");	// step 3 - associate ifile with filename

	struct players s[10];

	for (i = 1; i < 10; i++)
	{

		infile >> s[i].name >> s[i].position >> s[i].touchdowns >> s[i].catches >> s[i].passingYards >>
			s[i].receivingYards >> s[i].runningYards;
	}
	
	while (notquit)
	{

		printmenu();
		cin >> choice;
		switch (choice)
		{
		case 1:printData(s,10);
			system("pause");
			break;
		case 2:
		searchNamestr(s,10, searchName );
			system("pause");
			break;
		case 3:
			system("pause");
			break;
		case 0: notquit = 0;
			break;
		default:
			cout << "Wrong entry, please try again" << endl;
			system("pause");
		}
	}


	infile.close();					// step 5 - close input file
	//ofile.close();					// step 5 - close output file
	system("pause");
	return 0;
}

void printmenu()
{
	system("CLS");
	cout << "*******Please select your option*******" << endl;
	cout << "\n1. Display Player's Information ";
	cout << "\n2. Search a Player";
	cout << "\n3. Update a Player's Information";
	cout << "\n0. Exit" << endl;
	cout << "\nSelection: ";
	
}

void printData(struct players s[], int size)
{
	int i;
	for (i = 1; i<10; i++)
	{
		cout << "--------------------------------";
		cout << "\nName: " << s[i].name;
		cout << "\nPosition: " << s[i].position;
		cout << "\nTouchdowns: " << s[i].touchdowns;
		cout << "\nCatches: " << s[i].catches;
		cout << "\nPassing Yards: " << s[i].passingYards;
		cout << "\nReceiving Yards: " << s[i].receivingYards;
		cout << "\nRunning Yards:  " << s[i].runningYards;
		cout << "\n------------------------------" << endl;
	}

}
void searchNamestr(struct players s[], int size, string searchName)
{
	cout << "Enter the name of the player: ";


	cin >> searchName;

	int flag = 0;    // set flag to off

	for (int i = 1; i < 10; i++)    // start to loop through the array
	{
		if (s[i].name == searchName)   // if match is found
		{
			flag = 1;   // turn flag on
			break;    // break out of for loop
		}

	}

	
	for (int i = 0; i<10; i++)

		if (flag)    // if flag is TRUE (1)
		{

			cout << "The number of touchdowns " << s[i].touchdowns << ".\n" << "The Number of catches: " << s[i].catches << endl;
			
		}

		else
		{
			cout << "Sorry, I could not find that player in this array." << endl << endl;
		}
	}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int searchNamestr(struct players s[], int size) // int instead of void
{
	string searchName; // <-- Oops, sorry, left out originally by mistake
   cout << "Enter the name of the player: ";
	cin >> searchName;
	
	for (int i = 0; i < size; i++)    // start to loop through the array
	{
		if (s[i].name == searchName)   // if match is found
		{
			return i;
		}
	}
	return -1;
}


This returns the index number of the player, or -1 if none found. This gives you enough to process it further in main() etc.
Last edited on
for (int i = 1; i < size; i++) // start to loop through the array
That skips the first element of the array. Array indexing starts from 0, not 1.
closed account (48T7M4Gy)
Fixed. Total blame and recriminations with costs to OP.
Got it. Thanks!
Topic archived. No new replies allowed.