#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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;
}
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
@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
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
@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.