Hey there im doing a simple two up dos program for an assignment and im having some issues, we have been given a skeleton with methods and forms within it and im pretty sure were not allowed to change it. heres my code:

class TwoUp {
        Coin coin1 = new Coin();
        Coin coin2 = new Coin();
       
        
        void Play_TwoUp() {
            const int HEADS = 0;
            const int TAILS = 2;
            //const int ODDS = 1;
            int coin_one = coin1.Flip();
            int coin_two = coin2.Flip();
            int score = coin_one + coin_two;
            int player_score = 0;
            int house_score = 0;

            switch (score) {
                case HEADS:
                    Console.WriteLine("You threw Heads - You've won!");
                    player_score++;
                    break;

                case TAILS:
                    Console.WriteLine("You thre Tails - You loose.");
                    house_score++;
                    break;

                default:
                    Console.WriteLine("You threw Odds - Throw again.");
                    //Replay();
                    break;
            }
        }

        void Replay() {
            Play_TwoUp();
        }


        public TwoUp() {

        }



        public void Play(int menuOption) {
            Play_TwoUp();


        }


    } //end class TwoUp
}

My problem is that the int coin_one = coin1.Flip(); and coin2.Flip()
come up as cannot convert void to int, however i do have another game which required me to do the same and it has worked by doing it the same way. both are void methods.

Dani AI

Generated

, the error is exactly what and pointed out: if Flip() is declared void, it cannot be assigned to an int. Either return a value or have Flip() update state that you read afterward. If the skeleton forbids changing the signature, expose a readable property (or use an out parameter as mentioned) and map that to your scoring.

A clean pattern is to model the result, not magic numbers:

enum CoinFace { Heads = 0, Tails = 1 }

class Coin
{
    private static readonly Random rng = new Random(); // share one RNG
    public CoinFace Face { get; private set; }

    public void Flip()
    {
        Face = rng.Next(2) == 0 ? CoinFace.Heads : CoinFace.Tails;
    }
}

// usage
coin1.Flip();
coin2.Flip();
int total = (int)coin1.Face + (int)coin2.Face; // 0, 1, or 2
// 0 = two heads (win), 2 = two tails (lose), 1 = odds (throw again)

Two quick tips you might find helpful:

  • If you need to tally across multiple throws, do not declare player_score and house_score inside the play method. Make them fields so a replay does not reset them each call.
  • Avoid creating a new Random per flip; rapid successive calls with separate Random instances will repeat results. A single shared instance (as above) keeps outcomes uniform.

Recommended Answers

All 4 Replies

You don't show the Coin class so it's hard to tell what you need to do.

On the other hand, no where do you have code where you call a void method and store the result in an int. No where. Let me make it clear: No where. void means nothing is returned.

As Momerath says, void is void. It is not a value that can ever be returned, unless we confusing it with C/C++ (void *) which can be mapped onto any other pointer type.

you could try using an out or ref
void flip(out int iCoinState)
{
}

commented: Good point :) +7

I fixed the problem ages ago, I was in a rush so wasnt thinking properly haha wasnt ment to use the coin classes Flip, was ment to use another method to return a bool.

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.