Jump to content
  • Advertisement
Sign in to follow this  

String/Char issues

This topic is 4387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya I am having quite a fw problems using chars and string in my console applications. Naturally I use string for my class member. However std::cin.get seems only to get chars. Also I need to use a function that takes a "const char *". Could someone please tell me how to convert "char arrays" into strings, and how to convert strings into "const char *"s. "strcpy" and "strncpy" don't seem to work either. P.S. I'm working in C++.

Share this post


Link to post
Share on other sites
Advertisement

void func(const char*);
...
const char* str1 = "Hello World!";
char str2[] = "How's it going?";

std::string str3 = str1; // copies the contents of str1

str3 = str2; // copies the contents of str1, overwrites old contents of str3

std::cin >> str3; // reads one word into str3

std::getline(cin, str3); // reads a whole line into str3

func(str3.c_str()); // c_str() returns a const char* pointing to str3's contents

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!