Mur Quirk
Mur Quirk

Reputation: 171

C++: Pass variable from function outside main into function call inside main

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;
    }
}

Upvotes: 0

Views: 5849

Answers (2)

majinnaibu
majinnaibu

Reputation: 3032

You're using c++ so make a Flipper class that way you can abstract the flipCount from main entirely. This way you could completely change how the prompt and flipping work without main having to know at all.

class Flipper {
public:
    Flipper();
    void promptUser();
    void flipCoin(time_t seconds);
private:
    // This stores the data outside of your main function.
    int flipCount_;
}

Flipper::Flipper()
: flipCount_(0) {}

void Flipper::promptUser() {
    cout << "Enter flip count: " << endl;
    cin >> flipCount_;
}

void Flipper::flipCoin(time_t seconds) {
    for (int i=0; i < flipCount_; i++) {
        cout << rand() % (HEADS - TAILS + 1) + TAILS << endl;
    }
}

int main() {
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);

    Flipper flipper;  // Make a flipper
    flipper.promptUser(); // Prompt the user
    flipper.flipCoin(seconds); // Flip the coin
}

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

Simply return flipCount back to main and then let main pass it as an argument to flipCoin.

int main() {
  // ...
  // Get the flip count from the user
  int flipCount = promptUser();
  // Flip the coin that many times
  flipCoin(seconds, flipCount);
  // ...
}

int promptUser() {
  int flipCount;
  cout "Enter flip count: " << endl;
  cin >> flipCount;
  // Return the result of prompting the user back to main
  return flipCount;
}

void flipCoin(time_t seconds, int flipCount) {
  // ...
}

Think of main as being in charge. First main orders "Prompt the user for the number of flips!" and the promptUser function does as it's told, giving the number of flips back to main. Then main says "Now I know how many the flips the user wants... so flip the coin that many times!" passing that number to flipCoin to carry out the job.

main              promptUser      flipCoin
  |                   :               :
  |------------------>|               :
    "How many flips?" |               :
                      |               :
  |<------------------|               :
  |         3         :               :
  |                   :               :
  |---------------------------------->|
        "Flip the coin 3 times!"      |
                      :               |
  |<----------------------------------|
  |        <void>     :               :
  V
 END

Upvotes: 2

Related Questions