How to load information from a saved struct line by line
Jul 30, 2014 at 5:15am UTC
Hello !
Using some snippets from different coders and programs I have made the program below. My only issue is, is that when you enter your info, it then puts it all on a single line.
So if you enter:
it puts it all on one line, like so
Year Released: 1995mufasadraco104
Thank you for any and all help.
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 95 96 97 98
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Movie
{
string year;
string title;
string director;
string running;
};
int main()
{
Movie app;
//fstream textfile;
//textfile.open("Test.txt");
//textfile << app.year;
//textfile << app.title;
//textfile << app.director;
//textfile << app.running;
//textfile.close();
string saveload;
cout << "Welcome to the program!\n" << endl;
cout << "---------------------------" << endl;
cout << "| save - Save to a file |" << endl;
cout << "| load - Load from a file |" << endl;
cout << "---------------------------\n" << endl;
getline(cin, saveload);
if (saveload == "load" ) {
ifstream loadFile;
loadFile.open("Test.txt" );
loadFile >> app.year;
loadFile >> app.title;
loadFile >> app.director;
loadFile >> app.running;
cout << "The year of release:" ;
cout << app.year << endl;
cout << "The title of the movie:" ;
cout << app.title << endl;
cout << "The director's name:" ;
cout << app.director << endl;
cout << "The running time:" ;
cout << app.running << endl;
loadFile.close();
}
if (saveload == "save" ) {
cout << "Please enter the year of release:" ;
cin >> app.year;
cout << "Please enter the title of the movie:" ;
cin >> app.title;
cout << "Please enter the director's name:" ;
cin >> app.director;
cout << "Please enter the running time:" ;
cin >> app.running;
ofstream saveFile("Test.txt" );
saveFile << app.year;
saveFile << app.title;
saveFile << app.director;
saveFile << app.running;
saveFile.close();
}
cin.ignore();
cin.ignore();
return 0;
}
Jul 30, 2014 at 5:27am UTC
It doesn't do that when I run it.
Jul 30, 2014 at 5:38am UTC
You're not putting any whitespace between the items when you save the file:
1 2 3 4
saveFile << app.year << '\n' ; // <- you need to put whitespace after these
saveFile << app.title << '\n' ;
saveFile << app.director << '\n' ;
saveFile << app.running << '\n' ;
Jul 30, 2014 at 8:32am UTC
WOW, thank you so much!! I really appreciate it!
Topic archived. No new replies allowed.