Can anyone point me in the right direction of study?

Ok I am creating a hang man game, this is my first game.

But anyways On line 77 i am using a random number i earlier generated to access this random word in the file.

On line 79-85 is where i start to convert a string into a char array, this is where things start messing up,
i could not initally covert a string straight to char so i ended up using the .at() function.
Using WriteSubscript to iterate accessing each character in my random word


I put in some cout statements to see what I was actually copying, when i output the copy
transition on line 83 i get the follwing output on the console

Say for instance the word is cabbage i get an output of: 0 c1 a2 b3 b4 a5 g6 e

I am wondering how i can copy a string into a char array without abunch of funky text being included

Can anyone point me in the right direction?


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Prototype for loading in words from a file into a string array
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <ctime>
#include <stdio.h>
using namespace std;

//function prototypes
void WriteSubscripts();
void CurrentHangmanScreen(short int);

//global variables
short int CountCharacter = 0;  // short int CountCharacter = 0; // counts characters in word
short int WriteSubscript = 0;
char CopiedWord[16];
char LetterGuessForPlayer[16] ={ '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '\0'};
std::string wordsinarray[20];
short int RandomNumberObtained = 0; // this variable holds our random number that is generated
short int WrongGuesses = 0;   // Everytime a player gets it wrong add one to this variable 11 = being hanged
short int CorrectGuesses = 0;
char Letter;
char WantToQuit = 'n';

int main()
{


 short int wordscollected = 0; // counts the number of words in file




 srand(time(NULL)); // seed the time as starting random number

 // create variable to hold file, check is file opened is not corrupt

        ifstream myfile ("words.txt"); // creates a string variable named myfile to hold file contents
                 if (myfile.is_open()) // if myfile is open execte while bracket
                 {
                   while ( myfile.good() && wordscollected !=255)  // where 255 is, it sets max words allowed in word list
                   {
                     myfile >> wordsinarray[wordscollected];
                     //cout << wordsinarray[wordscollected] << endl;
                     wordscollected++;

                    }
                      myfile.close(); //close file and release memory after operation is done
                 }


                else cout << "Unable to open file";

   // end of file opening and adding words to string array

  cout << "There are " << wordscollected << " words In the file" << endl;

  // Generate the random number

  cout << "Picking random number " << endl;

  RandomNumberObtained = rand() % wordscollected; // divide random time number by wordscollected and put into RandomNumberObtained

  //For error checking purposes
  cout << RandomNumberObtained << endl;
   cout << "random word for today is " << wordsinarray[RandomNumberObtained] << endl;
  cout << "Chracters in this word are " << wordsinarray[RandomNumberObtained].size() << endl;
  CountCharacter = wordsinarray[RandomNumberObtained].size();
  cout << "CountChracterVariable: " << CountCharacter <<endl <<endl;

  //Write subscript symbol _ x times matching Count Character variable

  WriteSubscripts();


 //Copy the word into a char array CopiedWord

 for (WriteSubscript = 0; WriteSubscript < CountCharacter; WriteSubscript++)
 {
      cout << WriteSubscript;
      CopiedWord[WriteSubscript] = wordsinarray[RandomNumberObtained].at(WriteSubscript);
      cout << CopiedWord[WriteSubscript];
 }

 cout << endl;

// For Error Testing to see output of CopiedWord array elements from a string conversion

 for (WriteSubscript = 0; WriteSubscript < CountCharacter; WriteSubscript++)
 {
  cout << CopiedWord[WriteSubscript] << endl;
 }

 cout << endl;

 // write null character at end of string, which means it is the end of the string
 CopiedWord[WriteSubscript+1] = '\0';

 // test for checking if null character didnt overwrite word elements
/*
  for (WriteSubscript = 0; WriteSubscript < CountCharacter; WriteSubscript++)
 {
  cout << CopiedWord[WriteSubscript] << endl;
 }
*/

//main gameplay starts here
 while(WantToQuit != 'y')
 {
     cout << "Guess a Letter " << endl;
     cin >>  Letter;
  for (WriteSubscript = 0; WriteSubscript < CountCharacter; WriteSubscript++)
   {

       if (CopiedWord[WriteSubscript] == Letter)
       {
        LetterGuessForPlayer[WriteSubscript] = Letter;
        cout << "You got one";
        cout << "LetterGuessForPlayer:" << LetterGuessForPlayer[0] <<  cout <<    LetterGuessForPlayer[1] << LetterGuessForPlayer[2] << LetterGuessForPlayer[3] << LetterGuessForPlayer[4]<< LetterGuessForPlayer[5] << LetterGuessForPlayer[6] << LetterGuessForPlayer[7] << endl;
        cout << "Copied word"<< CopiedWord[0] << cout << CopiedWord[1] << cout << CopiedWord[2] << CopiedWord[3] <<  CopiedWord[4] << CopiedWord[5] << CopiedWord[6] << CopiedWord[7];
         if(LetterGuessForPlayer == CopiedWord)
         {
          cout<<endl<<"Yea, You survived and won!"; 
          }

       }
   }//closing for loop

 } //closing bracket for while

    cout << "Do you want to quit? ";
    cin >> WantToQuit;

return 0;
}


//Function writesubscripts

void WriteSubscripts()
{
     while(WriteSubscript < CountCharacter)
    {
      cout << LetterGuessForPlayer[WriteSubscript] << "\t";
      WriteSubscript++;
    }
    cout << endl;
}





Topic archived. No new replies allowed.