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
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.
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
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().
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.
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?
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:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h> std::srand is in cstdlib, std::time is in ctime
#include <string>
usingnamespace 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();
{
// ......
}
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.