I am making a program that asks the user to enter some information and then enters it into a text file, but I can't figure out how to read a whole line with spaces in it. When the user enters a line with spaces in it, only the first word appears in the text file. Can anyone help me figure this out? Thanks in advance.
Here is my program:
When I enter 2pac for artist and Greatest Hits for album, it skips the part where the user is supposed to enter the track and goes to the song so I enter the song I Get Around, and it skips the genre part.
Here is what it does:
Enter artist: 2pac
Enter album: Greatest Hits
Enter track: Enter song: I Get Around
Enter genre:
Here is the text file it outputs it to:
2pac
Greatest
Hits
I
Get
Can somebody please help me?
Here is my program:
Code:
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ofstream myfile("playlist.txt"); if(myfile.is_open()) { cout << "Enter artist: "; cin >> line; myfile << line << endl; cout << "Enter album: "; cin >> line; myfile << line << endl; cout << "Enter track: "; cin >> line; myfile << line << endl; cout << "Enter song: "; cin >> line; myfile << line << endl; cout << "Enter genre: "; cin >> line; myfile << line << endl; myfile.close(); } else cout << "Unable to open file."; return 0; }
When I enter 2pac for artist and Greatest Hits for album, it skips the part where the user is supposed to enter the track and goes to the song so I enter the song I Get Around, and it skips the genre part.
Here is what it does:
Enter artist: 2pac
Enter album: Greatest Hits
Enter track: Enter song: I Get Around
Enter genre:
Here is the text file it outputs it to:
2pac
Greatest
Hits
I
Get
Can somebody please help me?
Comment