if/else and switch

trying to make a fun little random selection program for family game night.
I want to use 2 choices: flip a coin and roll a die
if 1 is chosen, the program says something and flips the coin 1 time and the winner chooses what we play
if 2 is chosen, again the program dictates something, rolls the die and if the person chose the right number that was rolled, they choose what game we play.

im having trouble nesting the if/else or if/if statement within the switch case

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
 #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	
	cout << "Ha-Ha-Ha! Choose A Gate to Seal Our Fate!\n" << endl;
	cout << "Roll a Die, or Flip a Coin?" << endl;
	cout << "1: Flip" << endl;
	cout << "2: Roll" << endl;

	int option;
	cout << "Selection: ";
	cin >> option;

	switch (option)
	{
	case 1:
		cout << "Uh-Oh...\n";
		cout << "You Chose to Flip a Coin!\n";
		break;
	case 2:
		cout << "You Chose to Roll a Die\n";
		cout << "Hmm...Better Odds, Smart!\n";
		break;
	}
	system("pause");
}


I had this idea

1
2
3
4
5
6
7
8
switch(option)
{
  case():
    {if (1)
       {
          cout << //dictation
          cout << //dictation


after the dictation is where the program for the flip or roll would execute.

If that works, how would i then also integrate the rolling or flipping after the choice is made?
Last edited on
The switch code in your main() function looks fine and should function.

Edit: It's probably worth mentioning that the switch statement itself handles the conditional execution. When option == 1, everything between case 1: and break; will execute. No need for an imbeded if statement.

How comfortable are you with functions? This would be a great exercise to practice them.

1
2
3
4
5
6
7
8
9
10
11
switch (option)
{
    case 1:
        FlipCoin();
        break;
    case 2:
        RollDie();
        break;
    default:
        cout << "Invalid input" << endl
}


Using functions here helps avoid a large and complex switch statement. It'll make writing and testing the code for flipping a coin and rolling a die easier too.
Last edited on
@EtDecius
I read up on switch and if/else statements and realized that they are initially the same.
Here is Rev 2

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

	cout << "Ha-Ha-Ha! Choose A Gate to Seal Your Fate!\n" << endl;
	cout << "Roll a Die, or Flip a Coin?" << endl;
	cout << "1: Flip" << endl;
	cout << "2: Roll" << endl;

	int option;
	cout << "Selection: ";
	cin >> option;

	switch (option)
	{
	case 1:
		cout << "Uh-Oh...";
		cout << "You Chose to Flip a Coin!\n";

		int headsCounter, tailsCounter;

		headsCounter = 0;
		tailsCounter = 0;

		void coinFlip(void);
		{

			int coin;
			coin = rand() % 2;


			if (coin == 1)
			{
				headsCounter++;
				cout << "HEADS" << endl;
			}

			else
			{
				tailsCounter++;
				cout << "TAILS" << endl;
			}
		}

		int main();
		{
			for (x = 1; x <= 1; x++)

			{
				coinFlip();
			}

			cout << "Heads Landed" << headsCounter << "times" << endl;
			cout << "Tails Landed" << tailsCounter << "times" << endl;

			system("pause");
		}
	case 2:
		cout << "You Chose to Roll a Die\n";
		cout << "Hmm...Better Odds, Smart!\n";
		break;

	default:
		cout << "Incorrect Option\n";
	}
}


still working on die roll code. Does there have to be a 'default'?
Default is not required but is something you should always have.
Ok so i succeeded up until the die roll now. giving me an issue about using

#include <time.h>

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
case 2:
		cout << "You Chose to Roll a Die\n";
		cout << "Hmm...Better Odds, Smart!\n";

#include <time.h>
		
		int dieRoll();
		{
			int roll;
			int min = 1;
			int max = 6;

			roll = rand() % (max - min + 1) + min;

			return roll;
		}

		int main();
		{
			srand(time(0));
			for (int i = 0; i < 10; i++);
			{
				cout << dieRoll();
			}
		}

	default:
		cout << "Incorrect Option\n";
	}


the error points to line 89 which is int main()

error reads as follows:

Warning C4244 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data ClassTestingFloor
You're welcome to disregard my suggestion to use functions if you want. They're super important and useful so I recommend them but it's your call.

In your updated code, functions are not being defined or called correctly.
http://www.cplusplus.com/doc/tutorial/functions/

In short, the code for each function should be outside of main()

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
void flipCoin()
{
    // code to flip a coin goes here
}

int dieRoll()
{
    // code to roll a die goes here
}

main()
{
    cout << "Ha-Ha-Ha! Choose A Gate to Seal Your Fate!\n" << endl;
    ...

    switch (option)
    {
        case 1:
            coinFlip();
            break;
        case 2:
            dieRoll();
            break;
        default:
            cout << "Invalid input" << endl
    }

    return 0;
}



Your main() will call the other functions, but those functions should exist outside of (and in this case above) main().
Last edited on
@EtDecius
I missed the part where you asked about the functions.

So by putting the die roll, and coin flip codes outside of the main..im basically declaring what main should do when an option is chosen?
@EtDeciusI looked over my code and I see what you mean now. Now would relocating all of that outside and above main fix the time.h issue?
Correct. In this case, main() is primarily handling the menu and user input. The other functions can do the work associated with flipping coins and rolling die, which involves using a random number generator.

When you run a program, it starts at main() and goes line by line until main() is done. When it encounters a function call such as coinFlip(), main() is paused the program jumps to the function. The function's code is executed until it's done, then main() resumes where it left off.

The above code basically says run main() to get user input, then depending on the input call either coinFlip() or dieRoll(). Once the called function is complete, return to main(). At this point the program is done.
@EtDeciusI looked over my code and I see what you mean now. Now would relocating all of that outside and above main fix the time.h issue?

That depends on how things are moved around.

1
2
3
4
5
case 2:
		cout << "You Chose to Roll a Die\n";
		cout << "Hmm...Better Odds, Smart!\n";

#include <time.h>  // move this to the top with other #include 


I suspect that error is complaining about the #include <time.h> statement inside of a switch statement. I'm not knowledgeable enough to say if the compiler will reject it, but it's definitely out of place.
@EtDecius

This is the new updated code, it sort of works like it should. When i make a choice of 1 or 2 it just outputs the choice, but does not execute the roll or flip. Did i miss a function call somewhere?

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>

using namespace std;

void coinFlip()
{

	int coin, headsCounter, tailsCounter;
	coin = rand() % 2;
	headsCounter = 0;
	tailsCounter = 0;


	if (coin == 1)
	{
		headsCounter++;
		cout << "HEADS" << endl;
	}

	else
	{
		tailsCounter++;
		cout << "TAILS" << endl;
	}
}


int dieRoll()
{
	int roll;
	int min = 1;
	int max = 6;

	roll = rand() % (max - min + 1) + min;

	return roll;
}

int main()
{

	cout << "Ha-Ha-Ha! Choose A Gate to Seal Your Fate!\n" << endl;
	cout << "Roll a Die, or Flip a Coin?" << endl;
	cout << "1: Flip" << endl;
	cout << "2: Roll" << endl;

	int option;
	cout << "Selection: ";
	cin >> option;

	switch (option)
	{
	case 1:
		cout << "Uh-Oh...";
		cout << "You Chose to Flip a Coin!\n";
		
	case 2:
		cout << "You Chose to Roll a Die\n";
		cout << "Hmm...Better Odds, Smart!\n";

	default:
		cout << "Incorrect Option\n";
	}
	system("pause");
}


And I am pretty sure i fixed the time.h issue i was having.
nvrmnd..i found the snippets of code i was missing lol

had em buried safely in some comment sections of the code at the bottom lol
Last edited on
revised 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
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <string>

using namespace std;

int headsCounter = 0;
int tailsCounter = 0;
double headPct = 0;
double tailsPct = 0;

int coinToss(void) //random number generator so cout isd not always the same
{
	int randomNumber;
	randomNumber = 1 + rand() % 2;
	return randomNumber;
}


void coinFlip(void)
{

	int timesTossed = 0;
	int randomNumber = 0;
	string headTail = "";

	cout << "How Many Flips Would You Like?" << endl;
	cin >> timesTossed;

	srand((time(0)));
	for (int i = 1; i <= timesTossed; i++)
	{
		randomNumber = coinToss();
		if (randomNumber == 1)
			headTail = "HEADS";
		else
			headTail = "TAILS";

		cout << headTail << endl;
	}
}



int dieRoll()
{
	int x;
	srand(time(0));

	for (int i = 0; i < 1; i++)
	{
		x = (rand() % 6) + 1;
		cout << "Dice Throw Resulted in: " << x << endl;
	}

	cout << endl;
	return 0;
}

int main()
{

	cout << "Ha-Ha-Ha! Choose A Gate to Seal Your Fate!\n" << endl;
	cout << "Roll a Die, or Flip a Coin?" << endl;
	cout << "1: Flip" << endl;
	cout << "2: Roll" << endl;

	int option;
	cout << "Selection: ";
	cin >> option;

	switch (option)
	{
	case 1:
		cout << "Uh-Oh...";
		cout << "You Chose to Flip a Coin!\n";
		
	case 2:
		cout << "You Chose to Roll a Die\n";
		cout << "Hmm...Better Odds, Smart!\n";

	default:
		cout << "Incorrect Option\n";
	}
	system("pause");
}


no idea how to call the function when case 1 or 2 is selected
Hi,

It's better practise to put function declarations before main, then the function definitions after main. That way main will be near the top of the file, we don't have to go looking for it in a file with more 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h> std::srand is in cstdlib, std::time is in ctime
#include <string>

using namespace std;  // try to avoid doing this

// Avoid global variables
int headsCounter = 0;
int tailsCounter = 0;
double headPct = 0;
double tailsPct = 0;

// Function declarations
int coinToss(void); //  void has never been a requirement here in C++
void dieRoll(); // this should be void instead of always returning 0

int main()
{
//    ......
}

// Function definitions
int coinToss(); 
{
//    ......
}


void dieRoll(); 
{
//    ......
}
Last edited on
The coinToss() and dieRoll() functions can be called from within the switch statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (option)
{
case 1: 
    cout << "Uh-Oh...";
    cout << "You Chose to Flip a Coin!\n";
    coinFlip();
    break;
case 2:
    cout << "You Chose to Roll a Die\n";
    cout << "Hmm...Better Odds, Smart!\n";
    dieRoll();
    break;
default: 
    cout << "Incorrect Option\n";
}


If input == 1, some text is printed to the screen and coinFlip() is called, which prints the result. IF input == 2, some other text is printed and dieRoll() is called, which prints a different result.

The above should work, assuming your functions are correct. The design is kinda wonky and can be improved but I'd recommend getting it to work first.
Topic archived. No new replies allowed.