943,838 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3148
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 12th, 2007
0

getline cutting off input

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bapef is offline Offline
5 posts
since Jul 2007
Jul 12th, 2007
0

Re: getline cutting off input

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

c++ Syntax (Toggle Plain Text)
  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 :

C++ Syntax (Toggle Plain Text)
  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 ?
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006
Jul 12th, 2007
0

Re: getline cutting off input

Can't you instead use gets() function?

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 3
Junior Poster
krnekhelesh is offline Offline
127 posts
since Jul 2007
Jul 12th, 2007
0

Re: getline cutting off input

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 12th, 2007
0

Re: getline cutting off input

Click to Expand / Collapse  Quote originally posted by bapef ...
C++ Syntax (Toggle Plain Text)
  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*?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 12th, 2007
0

Re: getline cutting off input

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bapef is offline Offline
5 posts
since Jul 2007
Jul 12th, 2007
-2

Re: getline cutting off input

C++ Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 13th, 2007
0

Re: getline cutting off input

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

c++ Syntax (Toggle Plain Text)
  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 .
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006
Jul 13th, 2007
0

Re: getline cutting off input

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.
C++ Syntax (Toggle Plain Text)
  1. cin.getline(newcustomer->name, sizeof(newcustomer->name));

or like this
C++ Syntax (Toggle Plain Text)
  1. cin.getline(newcustomer->name,
  2. sizeof((struct customerType*)0->name));
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 13th, 2007
0

Re: getline cutting off input

Yes Dragon , Thanks for correcting me .
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Your code exhibits undefined and implementation-defined behavior
Next Thread in C++ Forum Timeline: I need a linking confirmation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC