Reading .txt file into program

I'm working with parallel arrays, one handles the titles and the other handles the ratings. Now everything works fine if the title doesn't start with a number, but if I decide to use a number infront of the title, when it reads it from the .txt file it thinks it is part of the rating.

I know why it is doing this, but don't know how to fix it.

Here is what I mean:

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
LOADING DATA FROM FILE...
MENU
1. Add Movie
2. Print All 
3. Exit
------------------------------------------------------
Enter 1-3 : 2
PRINT ALL
------------------------------------------------------
Hitch                                    98%     FRESH
Happy Gilmore                            99%     FRESH
Transformers 2                          100%     FRESH
SpongeBob                                10%    ROTTEN
MENU
------------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
------------------------------------------------------
Enter 1-3 : 1
ADD MOVIE
------------------------------------------------------
Title : 2 Fast 2 Furious
Rating : 100
MENU
------------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
------------------------------------------------------
Enter 1-3 : 2
PRINT ALL
------------------------------------------------------
Hitch                                    98%     FRESH
Happy Gilmore                            99%     FRESH
Transformers 2                          100%     FRESH
SpongeBob                                10%    ROTTEN
2 Fast 2 Furious                        100%     FRESH
MENU
------------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
------------------------------------------------------
Enter 1-3 : 3
SAVED TO FILE.


And this is what loads after the file has been saved:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
LOADING DATA FROM FILE...
MENU
1. Add Movie
2. Print All 
3. Exit
------------------------------------------------------
Enter 1-3 : 2
PRINT ALL
------------------------------------------------------
Hitch                                    98%     FRESH
Happy Gilmore                            99%     FRESH
Transformers 2                          100%     FRESH
SpongeBob                               102%     FRESH
 Fast 2 Furious                         100%     FRESH
MENU
------------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
------------------------------------------------------
Enter 1-3 : 


As you can see, the '2' from 2 Fast 2 Furious went to the rating in SpongeBob.

Here is what it looks like inside the .txt file:

Hitch| 98Happy Gilmore| 99Transformers 2| 100SpongeBob| 102 Fast 2 Furious| 100

Here 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
void SaveToFile(const string titles[], const int ratings[], int count)
{
	string filename;
	ofstream outfile;
	filename = "tomatoes.txt";
	outfile.open(filename);
	PrintToText(outfile, titles, ratings, count);
	WriteLine(cout, '=', 54);
	cout << "SAVED TO FILE.";
}

void PrintToText(ostream& out, const string titles[], const int ratings[], int count)
{
	for(int i = 0; i < count; i++)
    {
        out << titles[i] << '|' << " " <<  ratings[i];
	}
}

void LoadFromFile(string titles[], int ratings[], int& count)
{
    ifstream infile;
    infile.open("tomatoes.txt");
    if(!infile || !infile.is_open())
    {
        count = 0;
    }
    else
    {
        WriteLine(cout, '=', 54);
        cout << "LOADING DATA FROM FILE..." << endl;
        
        while( !infile.eof() )
        {
            getline(infile, titles[count], '|');
            infile >> ratings[count];
            count++;
        }
    }
}

void PrintAll(ostream& out, const string titles[], const int ratings[], int count)
{
	WriteLine(out, '=', 54);
	out << "PRINT ALL" << endl;
	WriteLine(out, '-', 54);
    
	for(int i = 0; i < count; i++)
	{
        out << setw(37) << left << titles[i]
        << right << setw(6) << ratings[i] <<  "%"
        << setw(10) << RatingToString(ratings, i) << endl;
	}
}


Any help would be greatly appreciated. Thank you.
Last edited on
Best way for doing this is keep all your data in Structures.Then you can save and retrive your data correctly
In structures? What do you mean by that?

So the way I'm doing it, it isn't possible?

Anyone else have any input on this?

Thank you.
I have resolved my previous question but have updated the first post with another one relating to my previous question.

Edit: Code added to first post.
Last edited on
Topic archived. No new replies allowed.