sometimes runs, sometime doesn't

Writing a basic rock paper scissors. problem occurs when C++ skips the switch case randomly. Sometimes it will run 5-9 times before skipping, sometimes it will skip on the very first fresh try. Have cut everything out but the switch cases and it still occasions to take the input, but not run any of the output and then terminates. Have no idea why its doing this. For example, it will prompt user to enter number, upon entering the number, it may or may not actually run the code for that input. This occurs seemingly randomly, as it is not attached to which input you enter or how many times it has run prior. Sometimes the first run wont work, but the second does. Sometimes the first 4 wont run, but then it will.

Thanks for any help!
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
 

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int CompChoice() //randomization and assignment of comp selection
{
	srand(time(0));
	int cChoice =1+ (rand()%3);
	return cChoice;
}

int main()
{
	//string answer;
	int answer=0;
	do{

	int pChoice=0;
	int cChoice=CompChoice();


	cout <<"Rock, Paper, Scissors"<< endl;
	//choices
	cout << "Your choices:"<< endl;
	cout << "1. Rock"<< endl;
	cout << "2. Paper"<< endl;
	cout << "3. Scissors"<< endl;
	cout << "4. Quit"<< endl;
	cout << "Enter your selection"<< endl;
	//get game position or end command
	cin >> pChoice;

//validate
	if (pChoice==4) //exit
		exit(0);
	else if (pChoice<=0 || pChoice>4) //invalid choices
		{
		cout<<"enter a valid selection, 1-4"<<endl;
		return 0;
		}


	else
	{switch(cChoice)
{
	case 0: //comp for rock
		if (pChoice==1)
			cout<<"You picked Rock, Comp picked Rock. Tie game" << endl;
		else if (pChoice==2)
			cout<<"You picked Paper, Comp picked Rock. You win!" << endl;
		else
			cout<<"You picked scissors, Comp picked Rock. You lose!"<< endl;
		break;

	case 1: //comp picks paper
		if (pChoice ==1)
			cout<<"You picked Rock, Comp picked Paper. You lose."<< endl;
		else if(pChoice==2)
			cout<<"You picked Paper, Comp picked Paper. Tie game"<< endl;
		else
			cout<<"You picked Scissors, Comp picked Paper. You Win!"<< endl;
		break;

	case 2: //comp picks scissors
		if (pChoice==1)
			cout<<"You picked Rock, Comp picked Scissors. You Win!"<< endl;
		else if (pChoice==2)
			cout<<"You picked Paper, Comp picked Scissors. You lose"<< endl;
		else
			cout<<"You picked Scissors, Comp picked Scissors. Tie Game"<<endl;
		break;

}
	}

	cout<<"Do you want to play again? Type 1 for yes"<< endl;
	cin >> answer;
	//}while(answer=="yes"||answer=="Yes"||answer=="YES");
	}while(answer==1);

return 0;}
srand() should be called just once.

The problem is that `CompChoice()' returns a value in the range [1;3], but your switch expects the range [0;2]
So changing line 13 to " int cChoice = (rand()%3);" should net me values 0-2, like the switch statement expects?

Srand should only be once? But I want ensure I have the computer taking diff selections for each game, so it doesnt follow one "randomization" for every game, but a fresh one at every choice
Topic archived. No new replies allowed.