read file error trouble

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

using namespace std;

struct Movie // movie information
{
	string name;  
	int year;
	double price;
};


int main()
{
	Movie list; // struct object

	cout << "The movies that we have on the list are" << endl;
	cout << "-----------------" << endl;

	
	string name;

	ifstream listfile; // reading file name
	listfile.open("Movie.txt");


	if (listfile.is_open())
	{
			while (listfile >> name) // reads data from file
			{
				cout << name << endl;
			}


		cout << " please enter the movie title you would like to put in the system" << endl;
		cin >> list.name;

		cout << "please enter the year the movie came out" << endl;
		cin >> list.year;

		cout << "please enter the price of the movie " << endl;
		cin >> list.price;
	}

	else
		cout << "unable to read file" << endl;
	
	listfile.close(); // close file 

	system ("pause");
	return 0;
}
Last edited on
Im having trouble with this program, i think it must be a silly mistakethat i am missing but i cant see it. I want the output of the file to show am i doing the if stament wrong or something any help will be greatly appreciated. Thank you
Please post a small sample of your input file.

What does your program output presently?

@jlb file name is "Movie.txt" but every time i run it it clears the text file for some reason, i think it is because i am not closing the file right i am a bit confused.


Title Year Price
------------------
Rival 2012 $12.45
Free Run 2000 $14.25
Followers 1998 $13.45
Members 3 1978 $13.20
Last edited on
I don't see anything in your current program that would erase the file contents. When I run the program I get the following output:

The movies that we have on the list are
-----------------
Rival
2012
$12.45
Free
Run
2000
$14.25
Followers
1998
$13.45
Members
3
1978
$13.20
please enter the movie title you would like to put in the system
test
please enter the year the movie came out
1922
please enter the price of the movie
50

Last edited on
@jlB I believe it might be the compiler or the IDE either way i will reinstall visual studio that might fix the problem. Thanks for the help i really appreciate it.
Topic archived. No new replies allowed.