Basic string input from screen

Posted on 2006-05-11
Last Modified: 2012-05-05

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";
Question by:Uzibat
    LVL 8

    Accepted Solution

    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'.
    LVL 8

    Expert Comment


    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.


    Author Comment

    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?
    LVL 8

    Expert Comment

    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.

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now