1 Replies - 475 Views - Last Post: 19 November 2012 - 06:54 AM Rate Topic: -----

#1 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

cin.getline not working

Posted 19 November 2012 - 06:35 AM

When i'm doing my code i got a problem.
When I use cin.getline(var,size,'\n');
it skips the line and goes to another line.

char name[50];
int number;

cin.getline(name,50,'\n');
cin>>number;

it skips the name line and goes to number. Why this is happen?
Is This A Good Question/Topic? 0
  • +

Replies To: cin.getline not working

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: cin.getline not working

Posted 19 November 2012 - 06:54 AM

Do you have input prior to this? Chances are there is a new line in the input buffer.

Try adding

#include <limits>

and use std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n' ); before the code you posted to eat up all the input currently in the input buffer, up to and including any new line character.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1