Search code examples
c#coin-flipping

C# - Keeping total number of correct guesses inside a variable


Beginner to programming & have been assigned a heads or tails coin flip project.

I've figured out how to make it randomly generate the number of requested flips and print out the answers. However, I'm supposed to have it add up how many times the user's input came up into the correctCount variable and I can't find anywhere how to do it. I've tried searching on here and different sites from searching throughout Google. I assume I need to take their string input and convert it somehow, like if result == string, then correctCount + 1 basically, but can't figure out how to make that happen since you can't do that with int & string. Any info or hints would be very helpful - thank you!

    class Coin
    {

        static void Main(string[] args)

        // UserInput
        {
            Console.Write("Guess which will have more: heads or tails?");
            string headsOrTailsGuess = Console.ReadLine() + "\n";
  

            Console.Write("\n" + "How many times shall we flip the coin? ");
            int numberOfFlips = int.Parse(Console.ReadLine() + "\n");


            // Declare variables
            int correctCount = 0;
            int heads = 0;
            int tails = 1;

            Random rand = new Random();

            for (int i = 0; i < numberOfFlips; i++)
            {
                int result = rand.Next(0, 2);

                if (result == 0)
                {
                    Console.WriteLine("Heads!");
                } 
                else 
                {
                    Console.WriteLine("Tails!");
                }
 
            }
            Console.WriteLine("Your guess, " + headsOrTailsGuess + "came up " + correctCount + " time(s).");```

Solution

  • As PMF mentioned, you have to not only receive user choice, but also analyse it. A simple comparison should be more that enough here, although you might want to add some validation to user input.

    static void Main(string[] args)
    
        // UserInput
        {
            int choice; //variable for parsed user choice
            Console.Write("Guess which will have more: heads or tails?");
            string headsOrTailsGuess = Console.ReadLine() + "\n";
            if(headsOrTailsGuess.ToLower().Trim() == "heads"){ //here you look if its heads
                choice = 0;
            }
            else{ //we can write additional if here to avoid other options from counting as tails
                choice = 1;
            }
    
            Console.Write("\n" + "How many times shall we flip the coin? ");
            int numberOfFlips = int.Parse(Console.ReadLine() + "\n");
    
    
            // Declare variables
            int correctCount = 0;
            int heads = 0;
            int tails = 1;
    
            Random rand = new Random();
    
            for (int i = 0; i < numberOfFlips; i++)
            {
                int result = rand.Next(0, 2);
    
                if (result == 0)
                {
                    Console.WriteLine("Heads!");
                } 
                else 
                {
                    Console.WriteLine("Tails!");
                }
                if(result == choice){ //comparing result to parsed choice and incrementing the counter
                    correctCount++; 
                }
    
            }
            Console.WriteLine("Your guess, " + headsOrTailsGuess + "came up " + correctCount + " time(s).");
    

    }