2

My Question/Problem:
Right now my program is using cin >> to get input and thats working fine except for I need to use getline() at one point and I know I shouldn't mix things like getline() and cin.get() with cin >>. How do I input an integer without using >>?

Research:
I've tried to research this, but every result I find says to use cin >> This is the closest I've found.

I would be just as happy to find a way to avoid using getline().

Community
  • 1
  • 1
StarSweeper
  • 397
  • 2
  • 4
  • 28

1 Answers1

5

There are a couple of ways:

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • The first option looks like what I'm looking for, but I'm confused about how to do it. Especially with using namespace std. This is what I've tried: `getline(cin,firstMiddleName); istringstream iss(firstMiddleName); iss >> firstMiddleName;` – StarSweeper Oct 12 '16 at 07:41
  • @StarSweeper That looks fine, what problems do you have with it? – Some programmer dude Oct 12 '16 at 07:48
  • iss gives the error incomplete type not allowed. I've never used istringstream before, I'm sure I'm missing a line or something. – StarSweeper Oct 12 '16 at 07:49
  • Yup. I needed #include this works, thank you! – StarSweeper Oct 12 '16 at 07:51
  • @StarSweeper Follow the link in my answer, it will tell you what header file you need. – Some programmer dude Oct 12 '16 at 07:51
  • This answer turned out not to work, but I'll leave it as accepted in case it works for someone else. Using @Bo Persson's answer in the comments ended up working. – StarSweeper Oct 12 '16 at 08:13
  • @StarSweeper If the answer doesn't solve your problem then you should not leave it as accepted. Remove it, it's okay. Just leave a comment about why for others. – Some programmer dude Oct 12 '16 at 08:18
  • This answer has the same issue as using mixing getline and >>, it skips the next input. This link has a good fix: http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input/10553849#10553849 – StarSweeper Oct 12 '16 at 08:24