Cannot pass string to function

Hi, below is part of my code for a simple Hangman Program which generates a random number and selects the word from a text file. This word is put in a string word. I apply the word.c_str (); feature to convert it from a string to a char array but to pass it to an array I had to create a new char array s[20] = {0}; and assign each element of the previous char array to the elements of s. What am I doing wrong. Is there a way of going around it without having to create all those arrays? Attached is the whole program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	
	int iSecret = 0;

	string word;
	
	srand ( time(NULL) );

	iSecret = (rand() % 13);
	
	string selc;

	if(fselc == 1)
	{
		selc = "names.txt";
	} else if (fselc == 2)
	{
		selc = "medium.txt";
	} else if (fselc == 3)
	{
		selc = "hard.txt";
	} else if (fselc == 4)
	{
		exit(0);
	}


	ifstream f(selc);
	
	for(int w = 0; w < iSecret; w++)
	{
		getline(f, word);
		//fseek(0, 12);
	}

	f.close();
	
	cout << word << endl;
	
	
	const char * r = word.c_str ();


	int iLength = strlen(r);

	char s[20] = {0};

	for (int i = 0; i < iLength; i++)
	{
		s[i] = r[i];
	}

	gotoxy(0, 0);
	
	cout << "Please try a word:" << endl;
	
	asterisk(iLength);

	trychar(s, iLength);


thanks
Ok passing string might look like this:

1
2
3
4
5
6
7
8
9
10
11
12
// function prototype
string& someThingToDo(string &inString); // this is for c++ style strings..

// for a char[20] pass I might do something like this
char* someThingtoDo(char *pString);  // this will take any char[] size
// it also might be expecting a string with a null last character type.

// I might call this like
char myString[20];

someThingtoDo(&myString[0]);


Another thing I don't understand why I am doing things like this. Why not just use the std::string for everything?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
std::string guessWord;
std::string matchWord;
std::string shieldedWord;

        ifstream f(selc);  // hmm I am not checking to see if the file was good...
	
	for(int w = 0; w < iSecret; w++)
	{
		getline(f, matchWord);
		//fseek(0, 12);
	}

	f.close();

        // astrisk the word
        // using shielded word I will assign '*' to it for the length of the match word.
        shieldedWord.assign("*", matchWord.length() );

       std::cout << "Please Make a guess:" << std::endl;
       std::cin >> guessWord;

       if(guessWord == MatchWord)
       {
             std::cout << "You match the word" << std::endl;
       }
       else
       {
             if(guessWord.length() == 1)
             {
                   bool bFound = false;
                   char searchChar = guessWord[0];

                   // search the match string for the searchChar
                   for(int nIndex = 0; nIndex < MatchWord.length(); nIndex++)
                   {
                          // at each time I find the search character
                          // I want to update the shieldedWord for that character
                          // and I want to know if I found anything.
                          if(matchWord[nIndex] == searchChar)
                          {
                                 bFound = true;
                                 shieldedWord[nIndex] = searchChar;
                          }
                   }
                   
                   if(bFound == true)
                   {
                          std::cout << "You Matched " << searchChar << " in the word." << std::endl;
                   }
                   else
                   {
                          std::cout << "You did not match any letter in the word."  << std::end;
                          std::cout << "Sadly, you rope is getting shorter" << std::endl;
                   }
              } 
              else // else guessWord > 1 in length.
              {
                    std::cout << "I am sorry that wasn't the word." << std::endl;
                    std::cout << "Sadly, you rope is getting shorter" << std::endl;
              }
      } // end the else on guessWord equaling matchWord 


This thread kind of goes through the entire game of hangman, which has a similar coding solution like the one I present here.
http://www.cplusplus.com/forum/general/42463/2/

Another thing if you want more on what std::string can do for you, you can look here:
http://www.cplusplus.com/reference/string/string/

std::string is an class/object and has a lot of the string functions in it for you to make life easier.
This part of the code


1
2
3
4
5
6
7
8
9
10
11
const char * r = word.c_str ();


	int iLength = strlen(r);

	char s[20] = {0};

	for (int i = 0; i < iLength; i++)
	{
		s[i] = r[i];
	}


can be replaced by

1
2
char s[20] = {0};
strcpy( s , word.c_str() ) ; 
Hi ,

1
2
3
4
5
for(int w = 0; w < iSecret; w++)
	{
		getline(f, matchWord);
		//fseek(0, 12);
	}


can be
1
2
3
4
 while( f.eof()  ) 
           {
                  getline( f, matchWord); 
           }



Last edited on
Topic archived. No new replies allowed.