Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Basic string input from screen

Hi

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?

Thanks!



#include <iostream>
#include <string>

using namespace std;

class CD
{
      public:
            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;
            }

      private:
            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: ";
      cin>>inputArtist;

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

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

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

      //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";
}
0
Uzibat
Asked:
Uzibat
  • 3
  • 2
1 Solution
 
List244Commented:
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:

cin.ignore();

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'.
0
 
List244Commented:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q240015

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 :
_I.rdbuf()->snextc();
Comment out the line and replace it with:
_I.rdbuf()->sbumpc;

And you will not longer have to press enter twice.

0
 
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?
0
 
List244Commented:
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.
0
 
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.

Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now