Help with files


The text in the file looks like this:

Name name2
2 1 2 3 4 M

I'm doing some sort of generic database with files ,i want to hold the full name of the person in a single string, so i used getline. That works fine when writing to the file ,but when i try to update a file with multiple persons the getline doesn't get the next line of text ,because it probably is still on the line with the " 2 1 2 3 4 M" data. How can i tell the computer to get the next line ,not the one it is on?


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
      cout<<endl<<"Enter student ID you want to update:";
    cin>>tempID;

    fstream flux("database.txt",ios::in);
    fstream temp("temp.txt",ios::out);

    while(!flux.eof())
    {
        getline(flux,data.name);
        flux>>data.id>>data.score1>>data.score2>>data.score3>>data.score4>>data.sex;
        if(tempID==data.id && !flux.eof())
        {
            cout<<endl<<"Student found,enter updated details:";
            cout<<endl<<"Name:";
            cin.ignore();
            getline(cin,data.name);
            cout<<endl<<"Give four scores:";
            cin>>data.score1 >>data.score2>>data.score3>>data.score4;
            cout<<endl<<"Enter sex(M or F):";
            cin>>data.sex;

            system("CLS");
            cout<<endl<<"Information updated!";
        }

        if(!flux.eof())
        {
            temp<<data.name<<endl<<" "<<data.id<<" "<<data.score1<<" "<<data.score2<<" "<<data.score3<<" "<<data.score4<<" "<<data.sex<<endl;
        }

    }
the getline doesn't get the next line of text

This getline()? --> getline(flux,data.name); (line 9)
Do you mean that the while loop doesn't give the impression to run? Have you already tried to 'cout' the data you collected to check what happens?
The loop does not function properly, i made a throw char before that getline and the code worked , but it messes up the first letter off all the other names.
Last edited on
1
2
3
4
5
6
7
    cout<<endl<<"Enter student ID you want to update:";
    cin>>tempID;
//...
            cout<<endl<<"Student found,enter updated details:";
            cout<<endl<<"Name:";
            cin.ignore(); //explain this
            getline(cin,data.name);



> i made a throw char before that getline and the code worked
if you do any modifications to your code, then post the updated code.
I think i put that there because the getline gets '\n' instead the name.Also i tried to do something with a throw but if i have more that one student it switches their first char from the name.


edit: here's the full code if it helps you http://cpp.sh/5zxd
Last edited on
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
Topic archived. No new replies allowed.