heads or tails

i am running the case statement to randomly pick either heads or tails but says its neither.
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string coin_quess;
	string coin;
	int score;
	score = 1;

	while (true)
	{

		cout << "coin toss simulator" << endl;
		cout << "Heads or tails: ";
		cin >> coin_quess;
		int question = rand() % 1;
		switch (question)
		{
		case 1:
			coin = "heads";
//test		//cout << coin << endl;
		case 2:
			coin = "tails";
//test		//cout << coin << endl;
		}
		if (coin_quess == coin)
		{
			cout << "CORRECT" << endl;
			score++;
			cout << score << endl;
		}
		else
		{
			cout << "INCORRECT" << endl;
			cout << score << endl;
		}
	}
	return 0;
}
Last edited on
closed account (j3Rz8vqX)
You'd want to mod by 2; causing it to modulate every time the value is 2 or more - forcing a value of less than 2 to be available.

Other than that, your above code should not compile because you're missing headers.

Also, you may to include a break; at line 24 before the next case, else it'll fall through to the second case.

Hope this helps.

Edit: Also, case should be 0 and 1; unless your expecting 1 and 2.
If 1 and 2, simply add 1 to the total rand after modding by 2.
Last edited on
If you try the following
1
2
3
4
5
6
7
#include <iostream>
int main()
{
 int i=5,j=6;
 std::cout<<i%1<<std::endl;
 std::cout<<j%1<<std::endl;
}

you notice that any integer%1 will return 0. a%1 is the reminder of the division of a by 1. Since a/1=a, that means the reminder is 0.

What you want is a%2, where the reminder is either 0 or 1 (not 1 and 2). If you want 1 and 2, then question=rand()%2+1

Make sure you include the header for rand
What headers am i missing because it compiles
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
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
	string coin_quess;
	string coin;
	int score;
	score = 0;

	while (true)
	{
		cout << "Coin Toss Simulator" << endl;
		cout << "Heads or Tails: ";
		cin >> coin_quess;

		//random coin flipper
		int question = rand() % 2;
		switch (question)
		{
		case 0:
			coin = "heads";
			break;
		case 1:
			coin = "tails";
			break;
		}

		//determins if quess is correct
		if (coin_quess == coin)//coin is the same a coin quessed
		{
			cout << "CORRECT" << endl;
			score++;
			cout << "Your score is: ";
			cout << score << endl;
		}
		else//if its anything else but  what coin is (heads/tails)
		{
			cout << "INCORRECT" << endl;
			cout << "Your score is: ";
			cout << score << endl;
		}
	}
	return 0;
}
dice version
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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <Windows.h>

using namespace std;

int main()
{
	string dice_quess;
	string dice;
	int score;
	score = 0;

	while (true)
	{
		cout << "dice Toss Simulator" << endl;
		cout << "Pick a number on the dice (1-6): ";
		cin >> dice_quess;
		cout << "Rolling dice" << endl;
		Sleep(1000);

		//random dice flipper
		int question = rand() % 6;
		switch (question)
		{
		case 0:
			dice = "1";
			break;
		case 1:
			dice = "2";
			break;
		case 2:
			dice = "3";
			break;
		case 3:
			dice = "4";
			break;
		case 4:
			dice = "5";
			break;
		case 5:
			dice = "6";
			break;
		}

		//determins if quess is correct
		if (dice_quess == dice)//dice is the same a dice quessed
		{
			cout << "CORRECT" << endl;
			score++;
			cout << "Your score is: ";
			cout << score << endl;
		}
		else//if its anything else but  what dice is (heads/tails)
		{
			cout << "INCORRECT" << endl;
			cout << "Your score is: ";
			cout << score << endl;
		}
	}
	return 0;
}
Topic archived. No new replies allowed.