-6

I'm trying to generate a random number and give the user 5 tries to guess the right number. I want to compare their guess and tell them if they're too high or too low. VS wont execute and I don't understand why I'm getting errors.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NumberGuessingGame
{
class Program
{
    static void Main(string[] args)

    {
        bool keepGoing = true;
        while (keepGoing)
        {
            Random rnd = new Random();
            int randomnumber = rnd.Next(1, 100);

            //PC tells user to pick a number, display guessed number


            Console.Write("Guess any integer between 1 and 100: ");
            int userentry = Console.ReadLine();


            {

                if (int.Parse(userentry > rnd.Next))
                { Console.WriteLine($"You entered {userentry}. That's too 
high. Try again.");
                    keepGoing = true;

                }

                if (int.Parse(userentry < randomnumber))
                {
                    Console.WriteLine($"You entered {userentry}. That's too 
low. Try again.");
                    keepGoing = true;
                }

                if (int.Parse(userentry = randomnumber))
                {
                    Console.WriteLine($"You entered {userentry}. That's 
right. Good job.");
                    keepGoing = false;
                }

                else (keepgoing) > 5);

                {
                    Console.WriteLine($"You've already tried 5 times. You 
lose.");
                    keepGoing = false;
                }

                else
                {
                Console.WriteLine("That is not a valid number.");
            }


        }
    }
}

}

DawnMoon
  • 1
  • 2
  • 2
    What errors are you getting? – juharr Mar 25 '17 at 20:12
  • Please post your errors. Currently I see at least one issue - `int.Parse()` is being given a Boolean expression, but should instead just be given `userentry`, e.g. `int.Parse(userentry) > randomnumber`. Also, the equivalence operator in C# is `==`, not `=`, so your `if` to check for equivalence should be: `if (int.Parse(userentry) == randomnumber)` – nb1987 Mar 25 '17 at 20:14
  • There are 5 distinct syntax errors here, that's far too many. You appear to be a complete beginner with C# so I recommend running through some basic tutorials before posting here. – DavidG Mar 25 '17 at 20:14

3 Answers3

2

First of all Console.ReadLine(); returns a string and not int. if you want to have an integer from the user input you need to convert it:

string input = Console.ReadLine();
int userentry = Convert.ToInt32(input);

second: you are handing int.Parse a bool as parameter. This does not work. In addition you forgot the ( ) behind the method Next You could either take the code from my post and rewrite your if statement like this:

if (userentry > rnd.Next)

or if you want to use int.Parse you can grab directly the user input:

if (int.Parse(Console.ReadLine()) > rnd.Next())

Third: this line :

else (keepgoing) > 5);

will be interpreted as a single command. An else statement does not have a conditional section. Since the compiler interprets it as a code line it tells you that you are trying to compare a bool with an int which does not work!

In short: you should take this condition for breaking out of the loop into your while statement and your programm will be very shortened. Up to now you rewrite the same steps unnecessarily in your loop. This makes the loop obosolete.

Random rnd = new Random();
int randomnumber = rnd.Next(1, 100);

int allowed_attemps = 5;
while (allowed_attemps > 0)
{
    //PC tells user to pick a number, display guessed number

    Console.Write("Guess any integer between 1 and 100: ");
    string input = Console.ReadLine();
    int userentry = Convert.ToInt32(input);

    if (userentry > randomnumber)
    {
        Console.WriteLine($"You entered {userentry}. That's too  high.Try again.");
    }
    else if (userentry < randomnumber)
    {
        Console.WriteLine($"You entered {userentry}. That's too low .Try again.");
    }
    else
    {
        Console.WriteLine($"You entered {userentry}. That's right.Good job.");
        break;
    }

    allowed_attemps--;
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

In this Example nothing can go wrong for you. If you guessing with an char instead of an int your program will crash. But with this example it will be zero if you enter an invalid input. I didn't have anything to do and i guess you have just started learning programming so i made up an example.

class Program
{
    static void Main(string[] args)
    {
        int numberOfTries = 0;
        bool keepGoing = true;
        Random rnd = new Random();
        int randomnumber = rnd.Next(1, 100);

        while (keepGoing)
        {
            //PC tells user to pick a number, display guessed number
            Console.Write("Guess any integer between 1 and 100: ");
            int userentry = 0;
            int.TryParse(Console.ReadLine(),out userentry);

            if (numberOfTries == 5)
            {
                Console.WriteLine($"You've already tried 5 times. You lose.");
                Console.ReadKey(); // A break, So you can see it before the application exit.
                keepGoing = false;
            }

            if (userentry > randomnumber)
            {
                Console.WriteLine($"You entered {userentry}. That's too high.Try again.");
                numberOfTries++;
            }

            if (userentry < randomnumber && randomnumber > 0)
            {
                Console.WriteLine($"You entered {userentry}. That's too low.Try again.");
                numberOfTries++;
            }

            if (userentry == randomnumber)
            {
                Console.WriteLine($"You entered {userentry}. That's right.Good job.");
                numberOfTries++;
            }

            else if(userentry == 0)
            {
                Console.WriteLine("That is not a valid number.");
            }
        }
    }
}

}

Mr Tangjai
  • 153
  • 2
  • 9
0

A few other miscellaneous issues:

if (int.Parse(userentry = randomnumber))

That's assignment, not comparison.

Technically, too, the way that you wrote it, userentry should already be an integer. Granted, you're trying to assign a string to it, but it's still supposed to be an int.

Also, == is a boolean expression, not something you can parse to an int. Furthermore, if (int.Parse....) fundamentally doesn't make sense; think about it - what would if (10)..., for example, mean?

Also, you really shouldn't call int.Parse every single time - just parse it once and store it in a variable.

Third, you should actually use int.TryParse here instead in case the user enters something that's not an integer. Right now, the program will just crash.

For cases like this, you should start at the first compile error and try to resolve them one at a time. Make sure you pay careful attention to the actual text of the error message. I'd suggest taking a look at How to Debug Small Programs.