Please Help

Good morning,
I am working on a project for school and it has stumped me. Would anyone be able to help me out with this issue I am having? Basically the first song input works perfectly; however, when I enter the second it skips "Enter the title of the second song" completely. I am not sure what I am doing wrong, but anything will help to clean this up. Thank you all.


#include <iostream>
#include <string>

using namespace std;

int main()
{

string pName;
string sTitle1, sTitle2, sTitle3;
string sArtist1, sArtist2, sArtist3;
string sGenre1, sGenre2, sGenre3;
float sLength1, sLength2, sLength3;
float totalLength;
float playlistAverage;


cout << "############################################################\n";
cout << " Welcome to the DJ MUSIC PlAYLIST DATABASE\n";
cout << "############################################################\n\n\n";


cout << "Insert a name for this playlist: ";
getline(cin, pName);


cout << "Insert the title of the first song: ";
getline(cin, sTitle1);
cout << "Insert the artist name of the first song: ";
getline(cin, sArtist1);
cout << "Insert the genre of the first song: ";
getline(cin, sGenre1);
cout << "Insert the length of the first song: ";
cin >> sLength1;


cout << "Insert the title of the second song: ";
getline(cin, sTitle2);
cout << "Insert the artist name of the second song: ";
getline(cin, sArtist2);
cout << "Insert the genre of the first song: ";
getline(cin, sGenre2);
cout << "Insert the length of the first song: ";
cin >> sLength1;
You are mixing stream extraction (cin >> sLength1;) which leaves the newline character ('\n') in the input stream and getline (which doesn't).

After your first cin >> sLength1; there is a (blank) end of line ... which gets picked up by the following getline() statement.


You can clear the rest of the line up to and including the newline with
cin >> sLength1; cin.ignore( 1000, '\n' );
(Nothing sacrosanct about the 1000 ... it just presumes you haven't sat there making that many spaces after the input)
Also, when you copy/paste code remember to change as required! genre of the second song? length of the second song and its variable?
Topic archived. No new replies allowed.