string pointers?

Aug 20, 2021 at 11:29pm
Hello, I am trying to create a hangman type game. The word to be guessed is going to be picked from a text doc, and the user inputs a string (either a single char or an entire word). I have been thinking of ways to handle everything, and I think I am going to need to check the word with the user input. I was thinking of using pointers to move through each of the strings and compare them. But I cant seem to figure out how to create a string pointer to the strings. I keep getting a type conversion error when i try, could someone please tell me if this is possible. And also, if it is possible is it a good way of doing this. I know there is a compare function in the stl, but was wanting to try and create it mostly without the use of stl methods.
Aug 20, 2021 at 11:56pm
you don't need any pointers for this. you could do it that way, but you do not need them.

we can show you what to do where you tried, show us what did not work if you want.

all you really need is
for each letter in the input
for each letter in the string
does it match? if yes, do something, else hang the sucker
string[int] is one character

string.cstr() or c_str() whatever it is (auto complete makes me lazy) gives a char*, but its constant so its about useless for most purposes.
&stringvar[0] gives a usable char*, but you really need to be careful if you do that and I do not advise it.
Last edited on Aug 21, 2021 at 12:11am
Aug 21, 2021 at 12:56am
std::string::c_str() returns a non-modifiable C string (pointer to a const null-terminated char array).
https://en.cppreference.com/w/cpp/string/basic_string/c_str

std::string::data() used to return a different pointer, C++11 changed that. data() and c_str() perform the same function now.
https://en.cppreference.com/w/cpp/string/basic_string/data

I've used the C string library strcpy() function to copy the data from the return of c_str() to a different C string so I have a modifiable C string when I need it.
https://en.cppreference.com/w/cpp/string/byte/strcpy
Aug 21, 2021 at 6:57am
Have a look at this code.
Maybe it gives you some ideas.
https://www.cppforschool.com/project/hangman-game-code.html
Aug 21, 2021 at 8:48am
data() and c_str() perform the same function now.
https://en.cppreference.com/w/cpp/string/basic_string/data


Before C++17, .c_str() and .data() returned const charT* for a non-const object. Since C++17 .data() now returns charT* and .c_str() still returns const charT*
Aug 21, 2021 at 8:56am
but was wanting to try and create it mostly without the use of stl methods.


Why? The STL is a big part of C++.

If you search through this forum you'll find examples of hangman.
Topic archived. No new replies allowed.