Hey i was trying to do a coin simulate that tells you how many heads / tails it appears and that every time heads pops up you gain $2.00 (win) and whenever tails pops up (lose) you lose $1.00. My program does not print the winnings i do not know why. Thanks

#include <iostream>
using namespace:std;

#include <iomanip>
using std: setprecision;

#include <cstdlib>

#include <ctime>

int main()
{
    double heads = 0;
    double tails = 0;
    int coin;

    srand (time(0) );

    for (int flip = 1; flip <= 10; flip++) {
        coin = 1 +rand()%2;

        switch (coin) {
            case 1:
                ++heads;
                break;

            case 2:
                ++tails;
                break;

            default:
                cout <<"?";
        }
    }
    cout <<"Heads was flipped "<<heads<<" times"<<endl;
    cout <<"Tails was flipped "<<tails<<" times\n"<<endl;


    return 0;
}

void calculateTotal(double heads, double tails)
{
    double win = heads * 2;
    double lose = tails * 1;
    double total = heads - tails;

    if (total > 0)
        cout <<"Congratulations you have won $ "<<total<<endl;
    else
        cout <<"Sorry you lost"<<endl;

}

Recommended Answers

All 5 Replies

you need to call calculateTotal(heads,tails); from main. As it is you don't do anything with it.

Here you create function specific variables that will be destroyed as soon as the function ends:

void calculateTotal(double heads, double tails)
{
double win = heads * 2;
double lose = tails * 1;
double total = heads - tails;

Therefore, you have no method of keeping record of how many heads and/or tails have appeared.

Couple of solutions: first, try saving the return value of the calculateTotal() function:

double = calculateTotal(double heads, double tails, int choice)
{
double win = heads * 2;
double lose = tails * 1;
//Beware, this could return a negative result which may confuse the user 
double total = heads - tails;
...
...
...
if(choice == heads)
    return win;

else return lose;

Another option would be just to declare a couple of variables (before int main(), or inside of int main(), doesn't matter) and pass them in by reference that the function can manipulate directly in memory:

void calculateTotal(double& heads, double& tails)
{

//Now you can perform operations directly on the heads/tails objects that reside in memory (as opposed to passing in 'copies' of the variables and then trying to save their return values which is obviously less efficient)
...
...
...

}

Also, remember that anytime you are keeping a 'running total', you might want to consider use the += "accumulation" operator.

He's not trying to accumulate anything, he does that in his for loop in main. Based on how he's laid it out, he just needs to pass in the heads and tails and get the output.

my bad teehee

No worries. Just thought I missed something huge for a second :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.