Junk number in first structure member?

In my program, I initialize the timesCalled member in the struct Movie to 0, then make a dynamically allocated array of structs with, of course, all the structs' timesCalled members initialized with 0. The problem is, when I display the timesCalled member for each structure to test it in lines 29 - 32, the first structure's timesCalled member is always some junk number. How do I fix this?

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

using namespace std;

struct Movie
{
    int timesCalled;
    int year;
    string name;
    string director;
};

Movie *readMovieInfo(int &size);
int binarySearch(Movie *array, int size, string value);
void printResults(int result, int size, Movie *array);

int main()
{
    int size, result = 0;
    string value;
    Movie *movieList = {0};     // Initialize timesCalled to 0

    movieList = readMovieInfo(size);

    for (int i = 0; i < size; i++)
    {
        cout << movieList[i].timesCalled << endl;
    }

    cout << "Here are the available directors:" << endl << endl;
    for (int i = 1; i < size; i++)
    {
        cout << left << setw(20) << movieList[i - 1].director << "\t";
        if (i % 3 == 0)
            cout << endl;
    }
    cout << endl << "Enter a director's name: ";
    getline(cin, value);

    result = binarySearch(movieList, size, value);
    printResults(result, size, movieList);

/*
    for (int i = 0; i < size; i++)
    {
        cout << movieList[i].year << "   ";
        cout << movieList[i].name << "   ";
        cout << movieList[i].director << endl;
    }
*/
    return 0;
}


Movie *readMovieInfo(int &size)
{
    ifstream inFile;
    string file;
    Movie *movieList;

    cout << "What is the name of the file you would like to open? ";
    getline(cin, file);
    file.append(".txt");
    inFile.open(file.c_str());
    if(!inFile)                                        // File validation
    {
        cout << "Can't open the input file!" << endl;
        exit(111);
    }

    inFile >> size;
    movieList = new Movie[size];       

    for (int i = 0; i < size; i++)
    {
        inFile >> movieList[i].year;

        getline(inFile, movieList[i].name, '\"');       
		getline(inFile, movieList[i].name, '\"');
		//movieList[i].name = movieList[i].name;

		getline(inFile, movieList[i].director, '\"');
		getline(inFile, movieList[i].director, '\"');
		//movieList[i].director = movieList[i].director;
    }
    inFile.close();

    return movieList;
}


The text file follows this format:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
16
1940    "Rebecca"                               "Alfred Hitchcock"
1945    "The Lost Weekend"                      "Billy Wilder"
1947	"Gentleman's Agreement"			"Elia Kazan"
1934	"It Happened One Night"			"Frank Capra"
1938	"You Can't Take It With You"		"Frank Capra"
1932	"Cavalcade"				"Frank Lloyd"
1953	"From Here To Eternity"			"Fred Zinnemann"
1941	"How Green Was My Valley"		"John Ford"
1950	"All About Eve"				"Joseph L. Mankiewicz"
1944    "Going My Way"                          "Leo McCarey"
1943	"Casablanca"				"Michael Curtiz"
1936    "The Great Ziegfeld"                    "Robert Z. Leonard"
1939	"Gone With the Wind"			"Victor Fleming"
1942	"Mrs. Miniver"				"William Wyler"
1945	"The Lost Weekend"			"William Wyler"
1946	"The Best Years of Our Lives"		"William Wyler"
Last edited on
1
2
3
4
5
6
7
8
9
10
    int size, result = 0;
    string value;
    Movie *movieList = {0};     // Initialize timesCalled to 0

    movieList = readMovieInfo(size);

    for (int i = 0; i < size; i++)
    {
        cout << movieList[i].timesCalled << endl;
    }


There are several things wrong with that.

First size is undefined Nevermind the first it is passed as reference and read into. Second You are not initalizing timesCalled to 0. You are telling the pointer it is null.

Then when you read in data you again leave TimesCalled as undefined. What exactly are you trying to do with this variable?

If you want to initalize it to zero you have to do either a brace-enclosed initializer list or moveList.timesCalled = 0;
Last edited on
I basically want to make all the timesCalled of the arrays to 0 because each time the user searches for a director, I want to increment timesCalled. It's basically a counter that stores the times the user called the certain director of the movie. But I figured that while I'm reading in the data here
1
2
3
4
5
6
7
8
9
10
11
12
   for (int i = 0; i < size; i++)
    {
        inFile >> movieList[i].year;

        getline(inFile, movieList[i].name, '\"');       
		getline(inFile, movieList[i].name, '\"');
		//movieList[i].name = movieList[i].name;

		getline(inFile, movieList[i].director, '\"');
		getline(inFile, movieList[i].director, '\"');
		//movieList[i].director = movieList[i].director;
    }


I might as well set movieList[i].timesCalled to 0 as it iterates through each movieList structure as you said at the end of your message. And it did work.
Last edited on
Topic archived. No new replies allowed.