On row 16
} data;
you are creating a global instance of the object ‘data’ calling it ‘data’...
Personal suggestion: change line 9 form
struct data
to
struct Student
or at lest to:
struct Data
That’s personal, anyway, maybe your perception is to make our code clearer that way.
If you add
#include <sstream>
you can modify your rows 177-180 from
1 2 3 4
|
while(!flux.eof())
{
getline(flux,data.name);
flux>>data.id>>data.score1>>data.score2>>data.score3>>data.score4>>data.sex;
|
to
1 2 3 4 5 6 7
|
string fbuffer;
while(getline(flux,fbuffer))
{
data.name = fbuffer;
getline(flux,fbuffer);
stringstream ss(fbuffer);
ss>>data.id>>data.score1>>data.score2>>data.score3>>data.score4>>data.sex;
|
and your code should work.
(The problem is there is a number of issues and maybe I had to change some of them and now I don’t remember what I did, I’m sorry.)
My output:
sh: 1: cls: not found
1. Add student records
2. Delete student records
3. Update student records
4. View all student records
5. Calculate an average of a selected student’s scores
6. Calculate total scores of a selected student
7. Display the highest and lowest scores
8. Sort students’ records by ID
9. Sort students records by total score
10. Erase all data
> 4
sh: 1: CLS: not found
Name: John I | ID: 1 | Score1:11 | Score2:12 | Score3:13 | Score4:14 | Sex:M
Name: John II | ID: 2 | Score1:21 | Score2:22 | Score3:23 | Score4:24 | Sex:M
Name: John III | ID: 3 | Score1:31 | Score2:32 | Score3:33 | Score4:34 | Sex:M
Name: John IV | ID: 4 | Score1:41 | Score2:42 | Score3:43 | Score4:44 | Sex:M
Name: John V | ID: 5 | Score1:51 | Score2:52 | Score3:53 | Score4:54 | Sex:M
Name: John VI | ID: 6 | Score1:61 | Score2:62 | Score3:63 | Score4:64 | Sex:M
Name: John VII | ID: 7 | Score1:71 | Score2:72 | Score3:73 | Score4:74 | Sex:M
Name: John VIII | ID: 8 | Score1:81 | Score2:82 | Score3:83 | Score4:84 | Sex:M
Name: John IX | ID: 9 | Score1:91 | Score2:92 | Score3:93 | Score4:94 | Sex:M
Name: John X | ID: 10 | Score1:101 | Score2:102 | Score3:103 | Score4:104 | Sex:M
sh: 1: pause: not found
sh: 1: cls: not found
1. Add student records
2. Delete student records
3. Update student records
4. View all student records
5. Calculate an average of a selected student’s scores
6. Calculate total scores of a selected student
7. Display the highest and lowest scores
8. Sort students’ records by ID
9. Sort students records by total score
10. Erase all data
> 3
sh: 1: CLS: not found
Enter student ID you want to update:2
Student found,enter updated details:
Name:Ann Smith
Give four scores:1001 1002 1003 1004
Enter sex(M or F):F
sh: 1: CLS: not found
sh: 1: pause: not found
sh: 1: cls: not found
Information updated!
1. Add student records
2. Delete student records
3. Update student records
4. View all student records
5. Calculate an average of a selected student’s scores
6. Calculate total scores of a selected student
7. Display the highest and lowest scores
8. Sort students’ records by ID
9. Sort students records by total score
10. Erase all data
> ^C |
This was my “database.txt” before executing the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
John I
1 11 12 13 14 M
John II
2 21 22 23 24 M
John III
3 31 32 33 34 M
John IV
4 41 42 43 44 M
John V
5 51 52 53 54 M
John VI
6 61 62 63 64 M
John VII
7 71 72 73 74 M
John VIII
8 81 82 83 84 M
John IX
9 91 92 93 94 M
John X
10 101 102 103 104 M
|
This is my “database.txt” after executing the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
John I
1 11 12 13 14 M
Ann Smith
2 1001 1002 1003 1004 F
John III
3 31 32 33 34 M
John IV
4 41 42 43 44 M
John V
5 51 52 53 54 M
John VI
6 61 62 63 64 M
John VII
7 71 72 73 74 M
John VIII
8 81 82 83 84 M
John IX
9 91 92 93 94 M
John X
10 101 102 103 104 M
|
Please consider:
- instead of calling menu() from every function, you could put your rows 33-97 into a while and simply return from every function.
- you could add an exit procedure :-)
Good luck