void function string c++

Hi,

I am totally stuck in my code and need a little bit of help.

I created my structure and now i am trying to create a function void that the user will enter a first name and it will look if it exists and if so it will display the result. I am just not sure which parameters to include in my function. Can you please help?

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
struct Student
{
	int idstudent;
	string firstname;
	string lastname;
	float Project;
	float Intra;
	float Final;
	int noteFinale;


void searchbyname(Student tabStudent[], )
{

	bool trouve = false;
	system("cls");

	for (int i = 0; i <? ; i++)
	{
		if ( tabEtudiant[i].lastname == ?)
		{
						
			trouve = true;
	// display code
			trouve = true;
			break;
			
		}

	}
	if (!trouve)
	{
		cout << "\n\This name was not found";
	}
	system("pause");
}
};
The function needs to know how many elements are in the array, and what it is supposed to be looking for.
void searchbyname(Student tabStudent[], int size, string name)

By a strange coincidence, these extra parameters are just the ones where you have a question mark in your code. :)
So after spending 15h on it I just realized I understood how to write the code but made a mistake in the void name. It was not the same in the header and in the APP :(

Thanks for your help!!
Line 18: You need to pass in the number of entries in the array as an argument for use in the termination condition.

Line 20: What is tabEtudiant? You're passing in tabStudent as the array argument.

I don't know if you've learned vectors yet. If you have, I would strongly recommend the use of std::vector instead of a raw array.

I would also suggest separating your input logic from your search function. If you do that, then you need to also pass the name to search for in as an argument. Or you can prompt for the last to search for inside the search function.

Line 26: You break out of the for loop when you find the first match? What is you have multiple students with the same last name?



closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

struct Student
{
	int idstudent;
	string firstname;
	string lastname;
	float Project;
	float Intra;
	float Final;
	int noteFinale;
};

Student searchbyname(Student[], int, string);

int main()
{
	Student studentList[] = { 
		0, "** not ", "found **",    0,    0,    0, 0,
		3,   "Emily",    "Frank", 2.30, 4.70, 8.41, 2
	};
	int studentCount = sizeof(studentList) / sizeof(Student);

	Student found;

	found = searchbyname(studentList, studentCount, "Frank");
	cout << found.firstname + found.lastname << endl;

	found = searchbyname(studentList, studentCount, "Fronk");
	cout << found.firstname << ' ' << found.lastname << endl;

	return 0;
}

Student searchbyname(Student tabStudent[], int noStudents, string aName)
{
	for (int i = 1; i < noStudents; i++)
	{
		if (tabStudent[i].lastname == aName)
			return tabStudent[i];
	}
	return tabStudent[0];
}


EmilyFrank
** not  found **
 
Exit code: 0 (normal program termination)


Another possibility. Student number as a primary key would overcome uniqueness but if a search was only conducted on name then the problem with duplicates ( a distinct possibility) could be addressed by returning a list of trouve'd students or simply displaying them in the search function at the time they are found by an additional line in the search function code.
Last edited on
Topic archived. No new replies allowed.