getline cutting off input

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2007
Posts: 5
Reputation: bapef is an unknown quantity at this point 
Solved Threads: 0
bapef's Avatar
bapef bapef is offline Offline
Newbie Poster

getline cutting off input

 
0
  #1
Jul 12th, 2007
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.
Last edited by bapef; Jul 12th, 2007 at 12:17 am. Reason: forgot something
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: getline cutting off input

 
0
  #2
Jul 12th, 2007
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

  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int main(void)
  5. {
  6. const int MAX =80;
  7. char full_name[MAX];
  8. cout<<"Enter your name: ";
  9. cin.get(full_name,MAX);
  10. cout<<"Hi !"<<full_name<<endl;
  11. cin.get();
  12.  
  13. return 0;
  14. }


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

  1. # include <iostream>
  2. # include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string full_name;
  8. cout<<"Enter your name: ";
  9. getline(cin,full_name);
  10. cout<<"Hi !"<<full_name<<endl;
  11. cin.get();
  12.  
  13. return 0;
  14. }

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 ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 126
Reputation: krnekhelesh is an unknown quantity at this point 
Solved Threads: 3
krnekhelesh's Avatar
krnekhelesh krnekhelesh is offline Offline
Junior Poster

Re: getline cutting off input

 
0
  #3
Jul 12th, 2007
Can't you instead use gets() function?

  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4. int main()
  5. {
  6. char name[25];
  7. cout<<"Enter the name of the customer: ";
  8. gets(name);
  9. cout<<name;
  10. }

gets(name); inputs the name with all the blanks and everything.Thats the difference between cin and gets.
Ubuntu Linux User #25732
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: getline cutting off input

 
0
  #4
Jul 12th, 2007
Originally Posted by krnekhelesh View Post
Can't you instead use gets() function?
No -- gets() is C not C++ and its terrible to use in C code too.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: getline cutting off input

 
0
  #5
Jul 12th, 2007
Originally Posted by bapef View Post
  1. newcustomer=new customerType;
  2. cout<<"Please enter a name: ";
  3. cin.ignore('\n');
  4. 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*?
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: bapef is an unknown quantity at this point 
Solved Threads: 0
bapef's Avatar
bapef bapef is offline Offline
Newbie Poster

Re: getline cutting off input

 
0
  #6
Jul 12th, 2007
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.
Last edited by bapef; Jul 12th, 2007 at 4:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: getline cutting off input

 
-2
  #7
Jul 12th, 2007
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. struct customerType
  5. {
  6. char name[21];
  7. int pin;
  8. float balance;
  9. customerType *link;
  10. };
  11.  
  12. void main(){
  13. customerType newcustomer ;
  14. cout<<"Please enter a name: ";
  15. cin.getline(newcustomer.name,21);
  16. cout <newcustomer.name;
  17. getch();
  18. }
Seems to work
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*
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: getline cutting off input

 
0
  #8
Jul 13th, 2007
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

  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. struct customerType
  5. {
  6. char name[21];
  7. int pin;
  8. float balance;
  9. customerType *link;
  10. };
  11.  
  12. void main()
  13. {
  14. customerType* newcustomer= new customerType;
  15. cout<<"Please enter a name: ";
  16. //cin.ignore('\n');
  17.  
  18. cin.getline(newcustomer->name,21);
  19.  
  20. cout<<newcustomer->name<<endl;
  21.  
  22.  
  23. }


one more thing is the third argument for getline is by default '\n' so that is also no need .
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: getline cutting off input

 
0
  #9
Jul 13th, 2007
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.
  1. cin.getline(newcustomer->name, sizeof(newcustomer->name));

or like this
  1. cin.getline(newcustomer->name,
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: getline cutting off input

 
0
  #10
Jul 13th, 2007
Yes Dragon , Thanks for correcting me .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC