Basic string input from screen


I'm literally just starting to learn C++ although I have programmed in a couple of other languages before. I'm trying to replace my cin statements with getline so that I can include spaces and multiple words in my strings.

However, when I replace cin>>inputArtist; with getline(cin, inputArtist);, I'm forced to hit return twice after my input to get it to display the next line. If I type something after the first return, it saves it in inputTitle anyway, even though it supposedly shouldn't have got to that code before it outputs the next line. The problem's compounded when I change further lines over to getline.

I've looked at various net sources but can't find anything that answers my (suuposedly) incredibly simple problem. I assume it's something to do with the cin buffer but don't know how to resolve it. Can anyone help?


#include <iostream>
#include <string>

using namespace std;

class CD
            CD();    // Default Constructor
            CD(string a, string t, int n, int y):artist(a), title(t), noOfTracks(n), year(y) {} //Constructor with values

            void setArtist(string newArtist)
                  artist = newArtist;
            string getArtist()
                  return artist;
            void setTitle(string newTitle)
                  title = newTitle;
            string getTitle()
                  return title;

            void setNoOfTracks(int newNoOfTracks)
                  noOfTracks = newNoOfTracks;
            int getNoOfTracks()
                  return noOfTracks;
            void setYear(int newYear)
                  year = newYear;
            int getYear()
                  return year;

            string artist;
            string title;
            int noOfTracks;
            int year;

void main()
      //define input holders
      string inputArtist;
      string inputTitle;
      int inputNoOfTracks;
      int inputYear;

      //get input for first class
      cout<<"Favourite album \n \n";

      cout<<"\n Please enter name of artist: ";

      cout<<"\n Please enter title of album : ";

      cout<<"\n Please enter number of tracks : ";

      cout<<"\n Please enter year of album : ";

      //create class with values
      CD myFavouriteCD(inputArtist, inputTitle, inputNoOfTracks, inputYear);

      //print out values of first class
      cout<<"\n \n Favourite album";

      cout<<"\n Artist : "<<myFavouriteCD.getArtist();
      cout<<"\n Title : "<<myFavouriteCD.getTitle();
      cout<<"\n Number of tracks : "<<myFavouriteCD.getNoOfTracks();
      cout<<"\n Year : "<<myFavouriteCD.getYear()<<"\n";
Who is Participating?
List244Connect With a Mentor Commented:
Uzibat, the problem of double-enter is actually a bug in the string header itself, if you want I could tell you how to fix it,
but it would only fix it for you.  That is if you program and compile on your computer, it will be fine, but anyone else with
the code will see the double-enter problem, as their compiler would still be bugged.

As for it skipping on the cin after a getline, you need to add:


after eaching cin.getline.  That is because otherwise there is a leftover '\n' in the buffer which is being passed to your other
variables.  That is why you are not getting input, because they already have the input they need, '\n'.

First, open you string file.  It is located in your VC98\include folder.
For example, my computer has it in: C:\Program Files\Microsoft Visual Studio\VC98\Include\string
Notice it has NO file extension, open it with notepad or whatever editor you like most.

Search for :
Comment out the line and replace it with:

And you will not longer have to press enter twice.

UzibatAuthor Commented:
Thanks for the prompt answer! It's good to know that it's not just my code that was causing the problem.

Suspected the buffer was the problem - seem to remember something similar in Java but there you could just read in twice to 'eat' up what was left in the buffer.

If I make the change to the file, once I've compiled the program, will the exe file then work ok on someone else's machine? It'll just be a problem running from the source code?
Yeah, the EXEs will be fine.  Just if you are in a class or something, and you have an assignment, know that your teacher
or whomever will use your SOURCE will have the same problem you used to.  Unless of course they fix their string too.
UzibatAuthor Commented:
Changed the line in the string file - everything works perfectly ad I can even take my cin.ignore() out after the cin.getline which makes my code simpler.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.