LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2004, 01:34 PM   #1
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Rep: Reputation: 30
Question spaces in a string


hi

im having some troubles when i have a user enter in a string with spaces:

string name;
string phonenumber;
string address;

cout << "name: ";
cin >> name;
cout << "phonenumber: ";
cin >> phonenumber;
cout << "address: ";
cin >> address;

when i enter in a name with a space in it, for example:

Billy Bob

it screws up and skips asking the phonenumber and jumps to the address

if anyone can help that would be awesome

thanks
 
Old 03-06-2004, 02:02 PM   #2
snacky
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 286

Rep: Reputation: 30
You might want to try getline
 
Old 03-06-2004, 02:09 PM   #3
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
hi thanks for the response snacky but i get an error when i try to use getline
//Seutp a few variables
string name;
string number;
string address;

//Get the information
cout << "Create a new entry\n" << endl;
cout << "Name > ";
getline(name);
cout << "Number > ";
getline(number);
cout << "Address > ";
getline(address);


and the error:

project1.cc:45: cannot convert `std::string' to `char**' for argument `1' to `
__ssize_t getline(char**, size_t*, FILE*)'
project1.cc:47: cannot convert `std::string' to `char**' for argument `1' to `
__ssize_t getline(char**, size_t*, FILE*)'
project1.cc:49: cannot convert `std::string' to `char**' for argument `1' to `
__ssize_t getline(char**, size_t*, FILE*)'

any suggestions?
thanks
 
Old 03-06-2004, 02:31 PM   #4
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Code:
string hello;
getline(cin,hello);
 
Old 03-06-2004, 02:33 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I think he meant cin.getline(), not the C function in stdio.h. If you need the results in a string instead of a char array, then use it like so:

Code:
char buffer[MAX_SIZE];
string name;

cout << "Name: " << endl;
cin.getline(buffer, MAX_SIZE);
name = buffer;
 
Old 03-06-2004, 02:39 PM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by Stack
Code:
string hello;
getline(cin,hello);
Oops... posted at the same time. Just tried this and it worked. Must be one of the template functions in STL? Never used this before. It's good to learn something new every once in awhile, maybe I should look at some of these functions a bit more. I've only ever really used a few of them.
 
Old 03-06-2004, 02:52 PM   #7
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
hrmm im still having some troubles


//Seutp a few variables
string name;
string number;
string address;

//Get the information
cout << "Create a new entry\n" << endl;
cout << "Name > ";
getline(cin,name);
cout << "Number > ";
getline(cin,number);
cout << "Address > ";
getline(cin,address);

when i run the program, it doenst wait for me to enter in a name

so if goes like this:

Name > Number >

it just skips the entering the name part

any suggestions?

thanks
 
Old 03-06-2004, 04:11 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Have you used cin previously to read in any other variables? Often times if you have code like so:

Code:
string s1, s2;

cin >> s1;
getline(cin, s2);
When it reads in s1, the LF (or CRLF pair) is still in the input buffer, so the getline() right afterwords reads up to THAT point, which often would result in an empty string....

Not sure I'm explaining myself very well, so as an example, for that first cin if you were to enter something like so:

myinput<return>

The first cin reads myinput into s1, leaving the <return> character, so that when the readline gets called, it finds that right away, so it reads a blank string into s2....
 
Old 03-06-2004, 04:15 PM   #9
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
oh ok so i use s1 as a temporary thingy that is there for cin so getline can work

thanks deiussum
 
Old 03-06-2004, 04:22 PM   #10
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
hrmm ok now it waits for me to enter iin a name but if i type in a name that has a space
for example;

Billy Bob;

it only takes:

" Bob"

into name
hahah i know ive been asking alot of quesions but its because i keep getting things wrong

any suggestions?

thanks
 
Old 03-06-2004, 05:34 PM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
No, that's not quite what I was saying to do. I was asking if you use cin that way anyplace before you do the getline, and trying to explain why that could cause a problem. It kind of seems like you might be, and if so, you could do a getline() after that in order to clear the return from the buffer.... So for example if you are doing something like so:

Code:
int n;
string name;
cout << "Enter number of entries: ";

cin >> n;

for (int c=0;c<n;c++)
{
     cout << "Enter name " << c << ": ";
     getline(cin, name);
     cout <<  "You entered \"" << name << "\"" << endl;
}
Enter number of entries: 3<return>
Enter name 0:
You entered ""
Enter name 1: This should be name 0?<return>
You entered "This should be name 0?"
Enter name 2: What is going on?<return>
You entered "What is going on?"

The stuff in bold is what you would have entered, the <return> is to show where you would have hit the Enter key...

What happened is that when you entered 3, cin << n ONLY read in the 3 and left the <return> in the input buffer.

So... when the next input used a getline, it returned right away because it read the <return> that was left over from entering the 3<return>....

To fix this, you could do something like this instead:

Code:
int n;
string name;
string temp;
cout << "Enter number of entries: ";

cin >> n;
getline(cin, temp); // Get rid of extraneous line feed

for (int c=0;c<n;c++)
{
     cout << "Enter name " << c << ": ";
     getline(cin, name);
     cout << "You entered \"" << name << "\"" << endl;
}
Is that clear yet?

Last edited by deiussum; 03-06-2004 at 05:39 PM.
 
Old 03-06-2004, 06:16 PM   #12
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
hello sorry if i was unclear deiussum

well in my souce i have:

//Seutp a few variables
string buffer;
string name = "";
string number = "";
string address = "";

//Get the information
cout << "Create a new entry\n" << endl;
cout << "Name > ";
getline(cin,name);
cout << "Number > ";
getline(cin,number);
cout << "Address > ";
getline(cin,address);

and i do not have any cin >> statments before

getline(cin,name);
 
Old 03-06-2004, 06:58 PM   #13
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Hmmm... ok.. that code should work if you aren't using cin like that anywhere before it is called. That means not just in the same scope as that code, but anywhere. Even if you had that code in a function, and had a cin >> something elsewhere before that function was called, you would run into that problem...

But, if you don't have anything like that ANYWHERE, I'm out of ideas of what might be wrong.
 
Old 03-06-2004, 07:36 PM   #14
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
oh yay!

i got it to work

i just added

cin.ignore();

before my first getline();

but i have not idea how i came up with that and why it works lol but it works!
 
Old 03-07-2004, 10:30 AM   #15
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
From MSDN's documentation of basic_istream::ignore()

Code:
Causes a number of elements to be skipped from the current read position.

basic_istream& ignore(
   streamsize _Count = 1,
   int_type _Delim = traits_type::eof( )
);
Parameters
_Count 
The number of elements to skip from the current read position. 
_Delim 
The element that, if encountered before count, causes ignore to return and 
allowing all elements after _Delim to be read. 
Return Value
The stream (*this).

Remarks
The unformatted input function extracts up to _Count elements and 
discards them. If _Count equals numeric_limits<int>::max, however, it is 
taken as arbitrarily large. Extraction stops early on end of file or on an 
element _Ch such that traits_type::to_int_type(_Ch) compares equal to 
_Delim (which is also extracted). The function returns *this.
So, if cin.ignore() is working, it further supports my theory that you have read something in prior to your getline, since it is skipping past a character. (Likely a linefeed character.)

Anyway, glad you got it working.

Last edited by deiussum; 03-07-2004 at 10:32 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problems in removing white spaces from string of text monil Programming 7 03-08-2005 11:28 AM
Bash - Strip Spaces from string then cat cmfarley19 Programming 8 07-25-2004 12:01 PM
Removing spaces from string qcoder Programming 3 07-05-2004 12:35 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration