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?
No -- gets() is C not C++ and its terrible to use in C code too.
Ancient Dragon
Retired & Loving It
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
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
#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
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