LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   spaces in a string (https://www.linuxquestions.org/questions/programming-9/spaces-in-a-string-154267/)

Longinus 03-06-2004 01:34 PM

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

snacky 03-06-2004 02:02 PM

You might want to try getline

Longinus 03-06-2004 02:09 PM

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

Stack 03-06-2004 02:31 PM

Code:

string hello;
getline(cin,hello);


deiussum 03-06-2004 02:33 PM

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;


deiussum 03-06-2004 02:39 PM

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.

Longinus 03-06-2004 02:52 PM

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

deiussum 03-06-2004 04:11 PM

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....

Longinus 03-06-2004 04:15 PM

oh ok so i use s1 as a temporary thingy that is there for cin so getline can work

thanks deiussum

Longinus 03-06-2004 04:22 PM

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

deiussum 03-06-2004 05:34 PM

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?

Longinus 03-06-2004 06:16 PM

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);

deiussum 03-06-2004 06:58 PM

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. :)

Longinus 03-06-2004 07:36 PM

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!

deiussum 03-07-2004 10:30 AM

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.


All times are GMT -5. The time now is 06:19 PM.