Hi everybody!

I'm a newbie to C++ and I need some help to improve my skills in c++.

*When you make input data "cafe monde" into a string variable, the variable only accept "cafe" and not the white space and "monde". What should I do to retrieve white spacem "monde" and "cafe" ini the same string variable after data input?

*Another obstacle I'm facing is to convert char to string. In the char it also can contain white space. I tried findin' the solution on Internet but I don't really understand the conversation or the code don't work.

/Fullmetalboy

u can use

getline()

, so assume you have declared an input stream object named "indata" like so

ifstream indata

and you have a string named "str", this is how you would read the input getline(indata, str);

use the getline function to get input with a space http://www.cplusplus.com/reference/string/getline/

What do you mean by "in the char it also contains white space"? What are you trying to accomplish?

I already had tried this sourcecode from "]http://www.cplusplus.com/reference/string/getline/" and it doesn't work. When I execute his sourcecode it just go through without askin' 'bout requestin' for data input. You can read "thank you, " but no data contain in the string variable str.

Sorry for my scriptural English. If you have char or cha a [8] with this value "aaa aa a". How should i do to convert it into string included with white space?

I already had tried this sourcecode from "]http://www.cplusplus.com/reference/string/getline/" and it doesn't work. When I execute his sourcecode it just go through without askin' 'bout requestin' for data input. You can read "thank you, " but no data contain in the string variable str.

Post the exact code that you used.

Sorry for my scriptural English. If you have char or cha a [8] with this value "aaa aa a". How should i do to convert it into string included with white space?

No big deal. You can use the constructor for string that takes in a const char *

char str[] = "hello";
string stdstr(str);

Edited 6 Years Ago by jonsca: n/a

Post the exact code that you used.

No big deal. You can use the constructor for string that takes in a const char *

char str[] = "hello";
string stdstr(str);

#include <iostream>
#include <string>

using namespace std;

int main ()

{
bool qualification = false;
int index = -1;

string persNr, wholeName, firstName, lastName, streetAddress, zipCode, city;

cout << "SSN (xxxxxx-xxxx): ";
cin >> persNr;


string str;
cout << "Firstname: ";
getline (cin,str);
cout << "Thank you, " << str << ".\n";

cout << endl;
cout << "Street address (inget nummer): ";
cin >> streetAddress;

cout << endl;
cout << "zip code: ";
cin >> zipCode;

cout << endl;
cout << "City: ";
cin >> city;

return 0;
}

Mixing getline and cin can lead to this problem (I thought you meant you compiled the example on the cplusplus.com site and it gave you trouble).

Put in a cin.ignore(); before the getline. There is an extra '\n' left in the stream after the prior cin that the getline() then reads in and then terminates thinking it's the end of the input.

When posting code please use code tags [code] //code here [/code]

Edited 6 Years Ago by jonsca: n/a

Mixing getline and cin can lead to this problem (I thought you meant you compiled the example on the cplusplus.com site and it gave you trouble).

Put in a cin.ignore(); before the getline. There is an extra '\n' left in the stream after the prior cin that the getline() then reads in and then terminates thinking it's the end of the input.

When posting code please use code tags [code] //code here [/code]

Thank you so much for you help!

This question has already been answered. Start a new discussion instead.