passing struct and if stream to function

trying to pass my struct and my if stream into my functions but keep receiving an error

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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<istream>

using namespace std;

const int NUM_STUDENTS = 29;
const int NUM_QUIZZES = 10;
const int NUM_EXAMS = 5;


struct Students {
	string firstName;
	string lastName;
	int quizzess[NUM_QUIZZES];
	int exams[NUM_EXAMS];
};

void progMenu(Students, fstream&);
void clearScreen();
string studentSearch(Students, fstream&);
int quizAverage();

int main() {
	
	Students students;
	
	ifstream roster;
	
    	roster.open ("C:/Users/D/Desktop/roster.txt");
	if(!roster)
	{
		cout << "Could not open input file. \n Exiting ..." << endl;
		system ("pause");
		exit(0);
    }
	
	progMenu(Students students, fstream& roster); // call function to display program menus
	

	system("pause");
	return 0;

}


void progMenu(){

	int menuSelect;

	cout << "Choose from the following options" << endl;
	cout << "\n";
	cout << "1. Display student grade (search by first or last name)" << endl;
	cout << "2. Display the average of a quiz (enter quiz number)" << endl;
	cout << "3. Display the average for an exam (enter exam number)" << endl;
	cout << "4. Display lowest quiz average" << endl;
	cout << "5. Display summary" << endl;
	cout << "6. Display letter grade breakdown" << endl;
	cout << "0. Exit..." << endl;
	cout << "\n"; //new line
	cout << "Selection: ";
	cin >> menuSelect;
	cout << "\n"; //new line

	switch (menuSelect) {

		case 1:
			studentSearch(Students students, fstream& roster);
			break;
		case 2:
			quizAverage();
			break;

		case 0:
			cout << "Exiting program..." << endl;
			cout << "\n";
			exit;

	}
	
}



string studentSearch(Students students, ifstream& roster){
	
	
	string name;
	
	cout << "Please enter student name: ";
	cin >> name;
	cout << "\n"; // new line

	if (name == students.firstName || name == students.lastName){
		cout << "Student exists" << endl;
	}
	else {
		cout << "This student does not exist in the roster" << endl;
	}
	return name;
}


int quizAverage() {
	int quizNum;
	cout << "This is the average..." << endl;
	return 3;
}


void clearScreen(){
	system("cls");
}
Last edited on
keep receiving an error
Which of the possible thousand error are you getting?

studentSearch(); There is no function studentSearch taking no arguments. You should pass Student and ifstream to it.
i re edit the code and theses are the errors im recieving.

41"expected primary-expression before "students"
41"expected primary-expression before '&' token
and the same for line 71 .
plus "roster" undeclared.
80 [Warning] statement is a reference, not call, to function `exit'
[Build Error] [main.o] Error 1



I believe im not declaring or passing the struct and the ifstream correctly into the function but since im pretty new to both concepts im at a loss on the proper way to pass the argument.
Last edited on
progMenu(Students students, fstream& roster); // call function to display program menus
1) this is not a function call. It is a declaration of new function.
    You need only pass names of the parametes, not their type:
1
2
void foo(int x); //declaration of function taking int.
foo(y); //Calling this function with some variable y as a parameter 

2) your progMenu does not take any arguments.

studentSearch(Students students, fstream& roster);
1) Same as (1) in previous.

exit;
Exit takes an argument
Line 41,71: Do not pass the types as part of the function call.

Line 22: Your prototype says progmenu takes a fstream reference, but you're trying to pass an ifstream at line 41.

Line 50: Your implementation of progMenu takes no arguments. This is contrary to your function prototype. They must agree.

Line 80: exit requires () and an argument. i.e. exit(0);
So with this layout below how would you pass the if stream from the main to the function ( or would you open the file in main at all?), then using struct search the file for a name? Im sorry for the inconvenience in advance. novice programming disorder


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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<istream>

using namespace std;

const int NUM_STUDENTS = 29;
const int NUM_QUIZZES = 10;
const int NUM_EXAMS = 5;


struct Students {
	string firstName;
	string lastName;
	int quizzess[NUM_QUIZZES];
	int exams[NUM_EXAMS];
};

void progMenu();
void clearScreen();
string studentSearch();

int main() {
	
	ifstream roster;
	
    	roster.open ("C:/Users/D/Desktop/roster.txt");
	if(!roster)
	{
		cout << "Could not open input file. \n Exiting ..." << endl;
		system ("pause");
		exit(0);
    }
	
	progMenu(); 
	

	system("pause");
	return 0;

}


void progMenu(){

	int menuSelect;

	cout << "Choose from the following options" << endl;
	cout << "\n";
	cout << "1. Display student grade (search by first or last name)" << endl;
	cout << "2. Display the average of a quiz (enter quiz number)" << endl;
	cout << "3. Display the average for an exam (enter exam number)" << endl;
	cout << "4. Display lowest quiz average" << endl;
	cout << "5. Display summary" << endl;
	cout << "6. Display letter grade breakdown" << endl;
	cout << "0. Exit..." << endl;
	cout << "\n"; //new line
	cout << "Selection: ";
	cin >> menuSelect;
	cout << "\n"; //new line

	switch (menuSelect) {

		case 1:
			studentSearch();
			break;
		case 0:
			cout << "Exiting program..." << endl;
			cout << "\n";
			exit(0);

	}
	
}



string studentSearch(){
	
	Students students;
	string name;
	
	cout << "Please enter student name: ";
	cin >> name;
	cout << "\n"; // new line

	if (name == students.firstName || name == students.lastName){
		cout << "Student exists" << endl;
	}
	else {
		cout << "This student does not exist in the roster" << endl;
	}
	return name;
}


void clearScreen(){
	system("cls");
}
anyone can help its been hours and my lack of knowledge has me stumped for a all nighter
1) Open file only in concrete functions.
Or
2) Make every function in call chain (progMenu and studentSearch) to take reference to istream and pass roster to it.
You were on the right track in your first post when you were passing student and roster. The only problem was that you were mixing fstream and ifstream type.

As to whether to open roster in main or elsewhere, that depends on how you're using it. If you're going to open it, read all the records and close it, then that is usually done in the same function. If you intend to open it then read it when you need to, then it makes sense to put the open in main. What you want to avoid is opening the file every time you want to look up a student. Opening a file can be expensive on some operating systems.
Topic archived. No new replies allowed.