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 |
You might want to try getline
|
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 |
Code:
string hello; |
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]; |
Quote:
|
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 |
Have you used cin previously to read in any other variables? Often times if you have code like so:
Code:
string s1, s2; 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.... |
oh ok so i use s1 as a temporary thingy that is there for cin so getline can work
thanks deiussum |
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 |
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; 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; |
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); |
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. :) |
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! |
From MSDN's documentation of basic_istream::ignore()
Code:
Causes a number of elements to be skipped from the current read position. Anyway, glad you got it working. |
All times are GMT -5. The time now is 06:19 PM. |