Specfic program crashing

I don't know why but this always crashes when I try to run it. It compiles just fine though. Since I'm learning from a book I don't know how to randomize yet
so I used some fixed strings and stored them in a vector. This is rock paper
scissors.

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
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}
int main() 
{  

int i = -1; // counter
string input; // user input
string result; // result of game
string yesorno; // option to contine playing
bool more = true; // for testing the input on yesorno

vector<string>computer(20);
computer[0] = "rock";
computer[1] = "paper";
computer[2] = "rock";
computer[3] = "scissors";
computer[4] = "rock";
computer[5] = "rock";
computer[6] = "scissors";
computer[7] = "paper";
computer[8] = "scissors";
computer[9] = "scissors";
computer[10] = "rock";
computer[11] = "paper";
computer[12] = "paper";
computer[13] = "scissors";
computer[14] = "rock";
computer[15] = "paper";
computer[16] = "rock";
computer[17] = "scissors";
computer[18] = "scissors";
computer[19] = "rock";
computer[20] = "rock";



while(more == true){
	++i;
	cout << "\n";
	cout << "Rock..Paper..Scissors.." << endl;
	cin >> input;
	cout << computer[i] << endl;
	// User inputs "rock"
	if (input == "rock" && computer[i] == "rock"){
		result = "Draw";
		}
	else if (input == "rock" && computer[i] == "paper"){
		result = "Lose";
		}
	else if (input == "rock" && computer[i] == "scissors"){
		result = "Win";
		}
	// User inputs "paper"
	else if (input == "paper" && computer[i] == "rock"){
		result = "Win";
		}
	else if (input == "paper" && computer[i] == "paper"){
		result = "Draw";
		}
	else if (input == "paper" && computer[i] == "scissors"){
		result = "Lose";
		}
	// User inputs "scissors"
	else if (input == "scissors" && computer[i] == "rock"){
		result = "Lose";
		}
	else if (input == "scissors" && computer[i] == "paper"){
		result = "Win";
		}
	else if (input == "scissors" && computer[i] == "scissors"){
		result = "Draw";
		}
	cout << result;
	cout << "Contine? Y/N" << endl;
	cin >> yesorno;
	
	if (yesorno == "Y"){
		more = true;
		}
	else if (yesorno == "N"){
		more = false;
		}
	if (i == 20){
		more = false;
		}
	}

}
Randomizing is super easy.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <random> //Library for random numbers. 

int i; 

i = rand() % 3; //Gives a random number 0-2
//i = rand() % 10; //Gives a random number 0-9 
//i = rand() % 20 + 1; //Gives a random number 1 - 20

if(i == 0)
{
    std::cout << "Paper.\n"; 
}
etc... 


As for your code:

You don't need any of this.

1
2
3
4
5
/*
#include<algorithm>
#include<cmath>

inline void keep_window_open(){char ch; cin>>ch;}*/


However, to use string you will need to #include <string> and to use cout/cin, you will need to #include <iostream>.

There is also no point in using a vector if you're not resizing; an ordinary array will suffice.

Your crash is caused by line 36.
 
computer[20] = "rock";


You declared a vector of size 20 and here you try to fill the 21st element. Remember, arrays begin at zero.



Topic archived. No new replies allowed.