Why is my code quitting?

When I enter "1" as the input for num_movies my code exits. Any ideas why?

~Henry

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

void time_read(double arr[6][10], int i, string movie[6]);

int main()
{
	int i(0);
	double arr[6][10];
	string movie[6];
	int num_movies;
	cout << "Please input the number of movies you'd like to watch: ";
	cin >> num_movies;
	while(i < num_movies)
	{
		time_read(arr, i, movie);
		i++;
	}
}

void time_read(double arr[6][10], int i, string movie[6])
{
	int j;
	for (j = 0; j < i; j++)
	{
		double time, hours, minutes;
		cout << "Enter the name of the movie followed by a #: ";
		getline(cin, movie[i], '#');
		cout << "Enter the movie time in hours then minutes: ";
		cin >> hours >> minutes;
		time = hours + minutes/60;
		arr[i][j] = time;
		
	}
}
Last edited on
I changed this...
time_read(arr, i, movie);
to this...
time_read(arr, num_movies, movie);
and it worked for me with just 1 movie...

but multiple movies were off... so you might need to relook what's going on in the time_read() function.
Henry,

It exits because you're initialising i to 0 in your main function, and then passing it into time_read(). In time_read, your loop condition is (j < i). If i is 0, and you initialise j to 0, then j is never less than i. This means that the body of your for loop won't be executed.

This also means that no matter what number you put in for num_movies, the first time you call time_read(), it will do nothing, because the first time it is called, i is 0.

Why do you need two loops? You have a while loop in main and a for loop in time_read(). What's the purpose of that?
Topic archived. No new replies allowed.