This is a little coin flip program I’m working on. I’m trying to pass a variable from function promptUser(); to flipCoin();. I know you can create a local variable inside main function, but i’d like to organize prompts into functions.
Is there a way to pass the flipCount value from the promptUser(); function to the flipCoin(); function?
I’ve spent some time on google looking for a way to do this (if there’s a way), but I don’t think I am able to express what I am trying to do correctly, or this just isn’t the way it’s done. However, if anybody understands what I’m trying to achieve, or why I shouldn’t do it this way, I would appreciate the advice. Thanks
#include <iostream>
#include <cstdlib>
#include <time.h>
// function prototype
void promptUser();
void flipCoin(time_t seconds);
// prefix standard library
using namespace std;
const int HEADS = 2;
const int TAILS = 1;
int main(){
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
promptUser();
flipCoin(seconds);
return 0;
}
void promptUser(){
int flipCount;
cout << "Enter flip count: " << endl;
cin >> flipCount;
}
void flipCoin(time_t seconds){
for (int i=0; i < 100; i++) {
cout << rand() % (HEADS - TAILS + 1) + TAILS << endl;
}
}
Simply return
flipCountback tomainand then letmainpass it as an argument toflipCoin.Think of
mainas being in charge. Firstmainorders “Prompt the user for the number of flips!” and thepromptUserfunction does as it’s told, giving the number of flips back to main. Thenmainsays “Now I know how many the flips the user wants… so flip the coin that many times!” passing that number toflipCointo carry out the job.