Having trouble outputting contents from pointer structure array

I'm having trouble with outputting the contents of a pointer struct array.
Below is my code provided:

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct movieInfo{

string id;
string title;
string actor1;
string actor2;
int released;
float rating;

};

void displayMenu();
void displayMovies(movieInfo *ptr, int);


int main(){

int choice;
const int MAX_MOVIE = 100;
movieInfo info[MAX_MOVIE];
movieInfo *ptr = new movieInfo[MAX_MOVIE];

ifstream inputFile;
inputFile.open("program2_library.txt");
if (!inputFile)
{
    cout << "ERROR: File not found! " << endl;

}
int total = 0;
while (inputFile.good()){
    getline(inputFile, ptr[total].id);
    getline(inputFile, ptr[total].title);
    getline(inputFile, ptr[total].actor1);
    getline(inputFile, ptr[total].actor2);
    inputFile >> ptr[total].released;
    inputFile >> ptr[total].rating;
    total++;
}
inputFile.close();
do{
    int movies = 4;
    displayMenu();
    cout << "Enter your choice: ";
    cin >> choice;

    switch(choice){
case 1:
    displayMovies(ptr, movies);
    break;
case 2:
    break;
case 3:
    break;
case 4:
     break;
case 5:
    break;
default:
    cout << "ERROR: choice not valid! " << endl;
    }

}
while (choice != 5);
return 0;
}
void displayMenu(){
cout << "Menu " << endl;
cout << endl;
cout << "1. Display movies sorted by title " << endl;
cout << "2. Display movies sorted by rating " << endl;
cout << "3. Lookup title and actors by id " << endl;
cout << "Lookup id by title and either actor " << endl;
cout << "Quit the program " << endl;

}
void displayMovies(movieInfo *ptr, int movies){
for (int i = 0; i < movies; i++){
    cout << ptr[i].id << " ";
    cout << ptr[i].title << " ";
    cout << ptr[i].actor1 << " ";
    cout << ptr[i].actor2 << " ";
    cout << ptr[i].released << " ";
    cout << ptr[i].rating << " " << endl;
}

}

Below is my output:
1
2
3
4
5
6
7
8
9
10
11
12
Menu

1. Display movies sorted by title
2. Display movies sorted by rating
3. Lookup title and actors by id
4. Lookup id by title and either actor
5. Quit the program
Enter your choice: 1
A201 Avengers: Endgame Scarlett Johansson Chris Evans 2019 8.6
Despicable Me 3 Steve Carell Kristen Wiig 2017 6.1
Neighbors Zac Efron Rose Byrne 2014 6.3 0 0
    0 0

My question is, why doesn't the movie ID's print for lines 2,3,4
and why does the fourth line output zeros rather than a movie description
from the text file? I've been stuck on it since yesterday and any input is greatly appreciated.
Try

1
2
3
4
5
6
7
8
9
10
int total = 0;
while (getline(inputFile, ptr[total].id)) {
    getline(inputFile, ptr[total].title);
    getline(inputFile, ptr[total].actor1);
    getline(inputFile, ptr[total].actor2);
    inputFile >> ptr[total].released;
    inputFile >> ptr[total].rating;
    inputFile.ignore(1000, '\n');
    total++;
}

Or a little better:

1
2
3
4
5
6
7
8
9
10
11
12
13
int total = 0;
string line;
while (getline(inputFile, line)) {
    if (total >= MaxMovie) break; // or whatever
    ptr[total].id = line;
    getline(inputFile, ptr[total].title);
    getline(inputFile, ptr[total].actor1);
    getline(inputFile, ptr[total].actor2);
    inputFile >> ptr[total].released;
    inputFile >> ptr[total].rating;
    inputFile.ignore(1000, '\n');
    total++;
}


I don't see why you have both 'ptr' and 'info'. 'info' would seem to be enough.
Also, 'movies' is just 'total'.
Last edited on
Tried the second option and it worked, i've revised it accordingly, thank you for reaching out!
Topic archived. No new replies allowed.