getting some problem in creating function and procedure in c++

hello guys, right now i try to write a problem in order to keep track student's grade using vector in c++. it should include one procedure(determin_grade), and three functions(average; find_highest; and find_lowest). however when i write the code, i can not the similar output as below (because i can not find out which part i need to fix):
Enter result “1” (or -1 if no more result): 55

Enter result “2” (or -1 if no more result): 90.4

Enter result “3” (or -1 if no more result): 110 (when calculate the average, and finding both lowest and highest, this number will not be counted. because it is out of the grade range that from 0 to 100)
Invalid Input!
Enter result “3” (or -1 if no more result): 74.60

Enter result “4” (or -1 if no more result): 34.6

Enter result “5” (or -1 if no more result): -1
Summary of the results:
Result 1: 55.00 Grade P
Result 2: 90.40 Grade A
Result 3: 74.60 Grade B
Result 4: 34.60 Grade U
The average of the results = 63.65
The lowest of the results = 34.60
The highest of the results = 90.40

please help figure out which part i need to change in order to get the similar output. thanks.
there is my code:

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>

#include <iomanip>

#include <vector>

using namespace std;

void determin_grade();

double averagevector(vector<double>);

double find_highest(vector<double>);

double find_lowest(vector<double>);

int main()

{ 

int num_result=5;

double input;

int index=1;

vector <double> grades; 

double average, highest,lowest; 

while (index <= 5) 

{ 

cout<<"Enter result "<<index<< "(or -1 if no more result): "; 

cin>>input; 

if (input >=0 && input<50) 

{ 

cout <<"Grade U will be assigned to this result" <<endl; 

    index++; 

} 

else if (input>=50 && input<60) 

{ 

cout <<"Grade P will be assigned to this result" <<endl; 

index++; 

} 

else if (input>=60 && input<70) 

{ 

cout <<"Grade C will be assigned to this result" <<endl; 

index++; 

} 

else if (input>=70 && input<90) 

{ 

cout <<"Grade B will be assigned to this result" <<endl; 

index++; 

} 

else if (input>=90 && input<=100) 

{ 

cout <<"Grade A will be assigned to this result" <<endl; 

index++; 

} 

else if (input> 100) 

{ 

cout <<"Invalid Input" <<endl; 

index; 

} 

else 

break; 

grades.push_back(input); 

} 

determin_grade(); 

average = averagevector(grades);

cout<<"average is "<<average<<endl; 

highest = find_highest(grades); 

cout<<"highest is "<<highest<<endl;

lowest = find_lowest(grades); 

cout<<"lowest is "<<lowest<<endl; 

return 0;

}

void determin_grade(vector<double> vectinput)

{ 

int index; 

int num_result=5; 

cout<<"summary of the result: \n"; 

cout<<fixed<<showpoint<<setprecision(2); 

for (index=0; index < num_result; index++) 

{ 
    cout<<"result "<<(index+1); 

    cout<<": "<<vectinput.size()<< endl; 
} 

}

double averagevector(vector<double> vectgrade)

{ 

double total = 0.0; 

double average; 

for(int index=0; index < vectgrade.size(); index++) 

{ 
      total += vectgrade[index]; 

      average = total/vectgrade.size(); 
} 

return average;

}

double find_highest(vector<double> vectgrade)

{ 

int index; 

double max = 0.0; 

for (index=0; index < vectgrade.size(); index++) 

{ 
   if (max<vectgrade[index]) 

     max = vectgrade[index]; 
} 

return max;

}

double find_lowest(vector<double> vectgrade)

{ 

int index; 

double lowest=0.0; 

for (index=0; index < vectgrade.size(); index++) 
{ 
    if (vectgrade[index]<=lowest)
    lowest = vectgrade[index]; 

} 

return lowest;

}

Firstly you've written 4 functions. I'm not sure why you are calling the function that returns void a "procedure". Is that a vb thing?

Anyway. The first issue is that you're declaring this:
void determin_grade();

But when you come to implement this function you're defining it like this:
void determin_grade(vector<double> vectinput)

Your compiler won't like that.

And then pass in your vector on line 107.

I would change your methods to take a const reference to your vectors as well, unless you are changing your vector in any of them.

Fix that then we can deal with your logic problems then. e.g line 142:
cout << ": " << vectinput.size() << endl;
This is not displaying your elements, you are just printing out the size of your vector 4 times.
Last edited on
i change the line 125 from the
void determin_grade(vector<double> vectinput)
to
void determin_grade()

i am not sure is it right or not. because i find similar format from some books, i think when calling the function of determin_grade, the format needs to keep same.
i think when calling the function of determin_grade, the format needs to keep same.

Yes. Which is what i told you. but your post above tells me you've done the opposite?

That function needs access to your vector so you have to pass it in.
Last edited on
there is my new code, and i can get the grades when calling the determin_grade function. however, the output still can not display the grades belong to A, B, C and so on, when calling the function. and also, the function of find_lowest still does not work. can you help me figure out the problem and give me some hits to fix them? thank you.


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 <iomanip>
#include <vector>
using namespace std;

void determin_grade(vector<double>);
double averagevector(vector<double>);
double find_highest(vector<double>);
double find_lowest(vector<double>);
int main()
{
	
	int num_result=5;
	double input;
	int index=1;
	vector <double> grades;
	double average, highest,lowest;

	while (index <= 5)
	{
		cout<<"Enter result "<<index<< "(or -1 if no more result): ";
		cin>>input;
		if (input >=0 && input<50)
		{
			cout <<"Grade U will be assigned to this result" <<endl;
		    index++;
		}
		else if (input>=50 && input<60)
		{
			cout <<"Grade P will be assigned to this result" <<endl;
			index++;
		}
		else if (input>=60 && input<70)
		{
			cout <<"Grade C will be assigned to this result" <<endl;
			index++;
		}
		else if (input>=70 && input<90)
		{
			cout <<"Grade B will be assigned to this result" <<endl;
			index++;
		}
		else if (input>=90 && input<=100)
		{
			cout <<"Grade A will be assigned to this result" <<endl;
			index++;
		}
		else if (input> 100)
		{
			cout <<"Invalid Input" <<endl;
			index;
		}
		else
			break;
		grades.push_back(input);
		

	}
	determin_grade(grades);
	average = averagevector(grades);
	cout<<"average is "<<average<<endl;

	highest = find_highest(grades);
	cout<<"highest is "<<highest<<endl;

	lowest = find_lowest(grades);
	cout<<"lowest is "<<lowest<<endl;

	return 0;

}
void determin_grade(vector<double> vect)
{
	int index;
	int grade;
	int num_result=5;
	cout<<"summary of the result: \n";
	cout<<fixed<<showpoint<<setprecision(2);

	for (index=0; index < vect.size(); index++)
	{
		cout<<"result "<<(index+1);
		cout<<": "<<vect[index]<< " grade: "<<endl;
		if (vect[index] >=0 && vect[index]<50)
			  grade = 'U';
		else if (vect[index]>=50 && vect[index]<60)
              grade = 'P';
		else if (vect[index]>=60 && vect[index]<70)
	          grade = 'C';
		else if (vect[index]>=70 && vect[index]<90)
		      grade = 'B';
		else if (vect[index]>=90 && vect[index]<=100)
		      grade = 'A';

	}
	
}


double averagevector(vector<double> vectgrade)
{
	double total = 0.0;
	double average;

	for(int index=0; index < vectgrade.size(); index++)
	{
		total += vectgrade[index];
		average = total/vectgrade.size();
	}
	return average;
}
double find_highest(vector<double> vectgrade)
{
	int index;
	double max = 0.0;
	for (index=0; index < vectgrade.size(); index++)
	{
		if (max<vectgrade[index])
		max = vectgrade[index];
	}
	return max;
}
double find_lowest(vector<double> vectgrade)
{
	int index;
	double lowest=0.0;
	for (index=0; index < vectgrade.size(); index++)
	{
		if (vectgrade[index]<lowest)
	    lowest = vectgrade[index];
		
	}
	return lowest;
}



by the way, i do not know why when i enter 110 which out of the range, it is still calculated in the function of average and find_highest.
Topic archived. No new replies allowed.