Problem with program "Stopped working" [Beginner]

Hello, so I was trying to make a c++ console game "Gallows" for school, and I've noticed, that my program (Its not the full game, its only beginning, but I can't do anything with this error.) doesn't run - Windows is showing "Program has stopped working message". Please help :/.
!!! The problem somehow starts when while loop begins.

Here's code:
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
# include <iostream>
# include <ctime>
# include <cstdlib>
# include <fstream>
# include <string>
using namespace std;

int main(){

    srand(time(0));

ifstream myFile("zodziai.txt");


int x;


myFile >> x;
int y = x;
string word[x];


    while(myFile>> word[x]){
    x--;
    }

    y = 1+rand()%y;
    string zodis = word[y];


    int ilg = zodis.length();
//^Word (Gets word from txt file. Text in that file is like this: 2(nr of words) word1 word2)
        string mask = zodis;
        int i = 1;
        while(ilg >= i){
        mask.replace(i-1,i,"*");
        i++;
}
//^Mask
char guess[7];
int lives = 7;
int a=0;
while(lives != 0&&mask!=zodis){

cout << "Zodis: " << mask << endl;
cout << "Spejimas: " ;
cin >> guess[a];


lives --;
a++;
}






return 0;
}
Last edited on
1)
1
2
3
myFile >> x;
//...
string word[x];
Is illegal C++. You should know array size in compile time.
2)
1
2
3
while(myFile>> word[x]){
    x--;
}
Firstly your first read is writing out of bounds: there is no word[x] element. Possible crash.
Second, there is no limit on amount of words read. If file does not contain exactly x words, your code will read data in elements -1, -2, etc. leading to crash

3) y = 1+rand()%y; can generate out of bound value again.
Hello, Thank you for quick reply, but it isn't the problem. I probably should have mentioned, that the code worked fine until while(lives != 0&&mask!=zodis) loop.

Those are problems. You just didn't run in them yet.
First one in particular prevents majority of people from even compiling your code, as non-standard extension usually is not used.

Last loop works fine and should not crash. How did you knew that problem is here?
What is your input?
Hmm I guess you are right :)
I thought that the problem was somewhere else because before that loop everything worked perfectly.
Nope. Just tested it where I replaced x with a number. Still doesn't work.
Did you fix all problems? mainly 2 and 3. Your fixed code works perfectly for me.

Post content of your file.
Try to get stack trace of the moment of your crash.
What IDE are you using?

Edit: your string replacement looks sketchy. second paremeter is how many characters to replace. Each iteration you are replacing more and more characters until i becomes larger than string size. This leads to exception.

Replace lines 33-37 with std::string mask(zodis.size(), '*'); And fix other out of range errors.
Last edited on
@MiiNiPaa
Thank you very much!
My code now runs perfectly. Here is the line that I've deleted: int ilg = zodis.length();.
Topic archived. No new replies allowed.