Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:07:26+00:00 2026-06-15T05:07:26+00:00

This is a little coin flip program I’m working on. I’m trying to pass

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:07:27+00:00Added an answer on June 15, 2026 at 5:07 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have taken this little snippet straight out of some code I'm working on:
i'm building this little function, what i need is this query to print me
I have this little problem, that I cannot figure out which arguments to pass
I've put together this little function in my android app: protected void toAddress(double lat,
In this little piece of code, what is the fourth line all about? from
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
I've got this little snippet of jQuery: $('#showlink').click(function(){ $('#linkwindow').show('fast'); $('#linkwindow input').focus(); } How do
This little OpenLayers.Control.EditingToolbar from is inserted by default: It's not really evident what these
This little line of code here is from a shopping cart: <a href=javascript:; onclick=simpleCart.add(
This little function checks a (finite) Brainfuck string for validity. It check's whether the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.