Entry number-fstream

Hello :)
I've made a program that writes at the end of the file base.txt the artist,the name and the genre of a song.I want that every entry should have a number.
Here is the 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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class song{
	public:
	string songname,artist,genre;
	song::song(string nsongname,string nartist,string ngen){
	songname=nsongname;
	artist=nartist;
	genre=ngen;
	
	cout<<"Entry created"<<endl;
	}
};
void writein(song newsong);

	
int main(){
string songname,artist,genre;

cout<<"Enter song name: ";
getline(cin,songname);
cout<<"Enter artist: ";
getline(cin,artist);
cout<<"Enter genre: ";
getline(cin,genre);

song newsong(songname,artist,genre);
writein(newsong);




system("pause");
return 0;
}

void writein(song newsong) {
    fstream base("base.txt",ios::out|ios::app);
	base<<"Song name: "<<newsong.songname<<endl;
	base<<"Song artist: "<<newsong.artist<<endl;
	base<<"Song genre: "<<newsong.genre<<endl;
	base<<endl;
}
	


The base.txt should look something like this:
1
2
3
4
5
6
7
8
9
1.Song name: dance the way i feel
Song artist: le nustiu
Song genre: party

2.Song name: one day
Song artist: wankelmut
Song genre: unknown

...

Thank you :)
nobody knows how to solve this ? :|
Well, your program currently outputs a single song to the file each time it is executed. It would be easier to add a number if the program had a loop, and the number is incremented each time.

But, if you close the program and start again, the numbering would re-start from 1. If you need the numbers in each subsequent run to continue from the previous numbering, it is a little more tricky. That would require opening the file for input, and reading/parsing the data to identify the last number used.

Below, quick example, but I wouldn't regard this as a polished program, just a quick test:
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class song{

public:
    string songname,artist,genre;

    song::song(string nsongname,string nartist,string ngen){
            songname=nsongname;
            artist=nartist;
            genre=ngen;
            cout<<"Entry created"<<endl;
    }
};
void writein(song newsong, int n);

int getNumber(string filename);

int main(){

    int n = getNumber("base.txt");
    cout << "last number was " << n << endl;

    string songname,artist,genre;

    cout<<"Enter song name: ";
    getline(cin,songname);
    cout<<"Enter artist: ";
    getline(cin,artist);
    cout<<"Enter genre: ";
    getline(cin,genre);

    n++; // next song number
    song newsong(songname,artist,genre);
    writein(newsong, n);

    system("pause");
    return 0;
}

void writein(song newsong, int number) {

    fstream base("base.txt",ios::out|ios::app);
    base << number << ". ";
    base<<"Song name: "<<newsong.songname<<endl;
    base<<"Song artist: "<<newsong.artist<<endl;
    base<<"Song genre: "<<newsong.genre<<endl;
    base<<endl;
}

int getNumber(string filename)
{
    ifstream fin(filename.c_str());
    if (!fin)
    {
        // unable to open file
        return 0;
    }

    int num = 0;
    string line;
    while (getline(fin,line))
    {
        // check if first character is numeric
        if (line.size() > 0)
            if (std::isdigit(line[0]))
                num = atoi(line.c_str());
    }
    return num;
}
Last edited on
Thank you,you rock !
I wouldn't ever thought at this.
Thank you again :D
one more question what is the difference between:
1
2
3
4
while(!base.eof()) {
getline(base,search);
//...
}

and
1
2
3
4
while(getline(base,search)) {
//...
}
		

?
one more question what is the difference between:

The first one is an unfortunately common error, the second one is normal C++ input loop.
so the first one is incorrect ?
Yes, "while(!something.eof())" is almost always incorrect (it can be worked around, but there's no point)
"eof()
Returns true if a file open for reading has reached the end."
why isn't it working:
1
2
3
4
5
6
while(!base.eof()) {
getline(base,search);
if(isdigit(search[0])){
poz=atoi(search.c_str());
}
}

i get string subscript out of range error.
i did this,it works:
1
2
3
4
5
6
7
8
9
10
while(1) {
getline(base,search);
if(search.empty()) {
break;
}
if(isdigit(search[0])){
poz=atoi(search.c_str());
	
}
}
When you're past the end of file (and you will go past the end of file with the first loop), getline() returns an empty string. It will also return an empty string when reading a, well, empty string - a line in a file that has only the newline character in it and nothing else (so your second loop may end well before the end of file)

search[0] attempts to access the first character of a string. You must be using some sort of debugging environment (such as visual studio's "Debug" build), and it lets you know when you're attempting to read a character from a string that has no characters.

i think i understood , thank you.
ok,so this is the last question here:
what does this do ? when does is stopps ? :
1
2
3
while(getline(base,search){
//...
}
while(getline(base, search)) stops when getline() fails (which is typically when it reads no characters due to the end of file condition, although other error conditions are possible -- note that while(!base.eof()), which runs past end of file once, also loops forever on any other error condition)
Last edited on
okey,thank you cubbi ,you saved me
Topic archived. No new replies allowed.