| | |
getline cutting off input
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
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.
Last edited by bapef; Jul 12th, 2007 at 12:17 am. Reason: forgot something
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
If you are using an instance of string data type means follow this approach :
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 ?
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
c++ Syntax (Toggle Plain Text)
# 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 :
C++ Syntax (Toggle Plain Text)
# 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?
gets(name); inputs the name with all the blanks and everything.Thats the difference between cin and gets.
C++ Syntax (Toggle Plain Text)
#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.
Ubuntu Linux User #25732
Last edited by Ancient Dragon; Jul 12th, 2007 at 6:10 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
C++ Syntax (Toggle Plain Text)
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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.
Last edited by bapef; Jul 12th, 2007 at 4:37 pm.
C++ Syntax (Toggle Plain Text)
#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(); }
Last edited by cscgal; Jul 13th, 2007 at 2:06 am. Reason: Removed color bbcode and added code tags
*Voted best profile in the world*
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
one more thing is the third argument for getline is by default '\n' so that is also no need .
It will work perfectly without cin.ignore() function .
If you want to access name using pointer means this code works
c++ Syntax (Toggle Plain Text)
#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.
or like this
C++ Syntax (Toggle Plain Text)
cin.getline(newcustomer->name, sizeof(newcustomer->name));
or like this
C++ Syntax (Toggle Plain Text)
cin.getline(newcustomer->name, sizeof((struct customerType*)0->name));
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Your code exhibits undefined and implementation-defined behavior
- Next Thread: I need a linking confirmation
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






