954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

getline cutting off input

The problem I am having is one in which I am using getline to read in a name with spaces, and when I attempt to output it it is cutting off the beginning of the names. This is the code I am using. I also have to have the cin.ignore('\n') else the program skips over the input completely.

newcustomer=new customerType;
cout<<"Please enter a name: ";
cin.ignore('\n');
cin.getline(newcustomer->name,21,'\n');


Any help is greatly appreciated as the project I am working ons deadline is approaching quick.

for example of what it does say I enter "john smith" minus the quotes, the output on the screen is just the 'h' at the end of smith nothing else.

bapef
Newbie Poster
5 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Hi bapef ,
I don't know whether you are using array of character or string data type for 'name'.

If you are using character array means get() function will help to get around this problem .

Here's an example

# include <iostream>
using namespace std;

int main(void)
{
    const int MAX =80;
    char full_name[MAX];
    cout<<"Enter your name: ";
    cin.get(full_name,MAX);
    cout<<"Hi !"<<full_name<<endl;
    cin.get();

    return 0;
}

If you are using an instance of string data type means follow this approach :

# include <iostream>
# include <string>
using namespace std;

int main(void)
{
    string full_name;
    cout<<"Enter your name: ";
    getline(cin,full_name);
    cout<<"Hi !"<<full_name<<endl;
    cin.get();

    return 0;
}


Hopes it's helpful for you .

but I have one question if we execute the above code in Microsoft visual c++ compiler means i have to enter two times to input name where as i works perfectly in Devc++ compiler I don't why
can any one clarify me ?

parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6
 

Can't you instead use gets() function?

#include<iostream.h>
#include<conio.h>

int main()
{
  char name[25];
  cout<<"Enter the name of the customer: ";
  gets(name);
  cout<<name;
}


gets(name); inputs the name with all the blanks and everything.Thats the difference between cin and gets.

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 
Can't you instead use gets() function?

No -- gets() is C not C++ and its terrible to use in C code too.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
newcustomer=new customerType;
cout<<"Please enter a name: ";
cin.ignore('\n');
cin.getline(newcustomer->name,21,'\n');

for example of what it does say I enter "john smith" minus the quotes, the output on the screen is just the 'h' at the end of smith nothing else.

post the customerType structure -- how did you declare name? Since this is c++ why don't you use std::string instead of char*?

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

parthiban I tried using cin.get() and I still end up with it cutting off most of the name. Its really weird and I know once I see the solution I am going to kick myself for its simplicity.

Ancient Dragon here is my struct declaration,
struct customerType
{
char name[21];
int pin;
float balance;
customerType *link;
};

I used a char array because there is a fixed ammount of room for the customers names.

bapef
Newbie Poster
5 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 
#include <iostream.h>
#include <conio.h>

struct customerType
{
char name[21];
int pin;
float balance;
customerType *link;
};

void main(){
customerType newcustomer ;
cout<<"Please enter a name: ";
cin.getline(newcustomer.name,21);
cout <newcustomer.name;
getch();
}

Seems to work

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I think you are using cin.ignore() before cin.getline() function. Avoid it and try .
It will work perfectly without cin.ignore() function .

If you want to access name using pointer means this code works

#include <iostream.h>
#include <conio.h>

struct customerType
{
    char name[21];
    int pin;
    float balance;
    customerType *link;
};

void main()
{
    customerType* newcustomer= new customerType;
    cout<<"Please enter a name: ";
    //cin.ignore('\n');

    cin.getline(newcustomer->name,21);

    cout<<newcustomer->name<<endl;

    
}

one more thing is the third argument for getline is by default '\n' so that is also no need .

parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6
 

You should not hardcode the size of the name array as shown on line 18 but use the sizeof operator so that if you change the size of the array you don't have to worry about changing it in more than one spot.

cin.getline(newcustomer->name, sizeof(newcustomer->name));


or like this

cin.getline(newcustomer->name, 
   sizeof((struct customerType*)0->name));
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yes Dragon , Thanks for correcting me .

parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6
 

I figured out what was wrong, I needed to change the cin.ignore('\n'); to cin.ignore(100,'\n'); I had to have that in there less it skip over the input entirely, reason for that was there was a left over newline character in the input buffer, as well as someother junk that was throwing off the array.

bapef
Newbie Poster
5 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You