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.

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 ?

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.

commented: Nope:) -2
commented: There are better methods of accepting user input. Read the C/C++ tutorials of this site. +24

Can't you instead use gets() function?

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

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*?

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.

Member Avatar for iamthwee
#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

commented: void main? :P -3
commented: did not use code tags, inappropriate use of code tags -3

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 .

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));

Yes Dragon , Thanks for correcting me .

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.