9 Replies - 1205 Views - Last Post: 19 July 2016 - 01:05 PM Rate Topic: -----

#1 Skidkidd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 16

Random Number game C#

Posted 19 July 2016 - 10:38 AM

Code runs but the variable guess is reading incorrectly. if I type in 6 as my guess the actual value guess gets is 54.
I added a picture of the guess value.

namespace Think_of_a_number
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int counter = 3; //lives
            Random rnd = new Random();
            int number = rnd.Next(1, 11);
            int guess;
            int point = 0;

            while (counter > 0)
            {
                Console.Clear();
                Console.WriteLine("You have " + counter + " lives, and " + point + ".");
                Console.ReadLine();
                Console.WriteLine("Guess what number " + number +" I am thinking of between 1 and 10");
                guess = Console.Read();

                if (guess == number)
                {
                    Console.WriteLine("You're Right! You get 1 point!");
                    Console.ReadLine();
                    point++;
                }
                else
                {
                    Console.WriteLine("Wrong Try again.");
                    Console.ReadLine();
                    counter--;
                }
                
            }


        }
    }
}

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Random Number game C#

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Random Number game C#

Posted 19 July 2016 - 10:40 AM

54 is the ASCII for 6
You're grabbing the ASCII code for the typed character, not the character itself.
https://www.google.c...31hF4tTw886M%3A
Was This Post Helpful? 0
  • +
  • -

#3 Skidkidd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 16

Re: Random Number game C#

Posted 19 July 2016 - 10:46 AM

View PosttlhIn`toq, on 19 July 2016 - 10:40 AM, said:

54 is the ASCII for 6
You're grabbing the ASCII code for the typed character, not the character itself.
https://www.google.c...31hF4tTw886M%3A


Well that makes sense, but now I have to ask how do I grab the character and still use it in my if statement?
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,209
  • Joined: 12-January 10

Re: Random Number game C#

Posted 19 July 2016 - 10:54 AM

i would do something like

string guessInput = string.empty;
guessInput = Console.Read();
guess = int.Parse(guessInput);



not tested but you get the idea
Was This Post Helpful? 1
  • +
  • -

#5 Skidkidd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 16

Re: Random Number game C#

Posted 19 July 2016 - 11:17 AM

I didn't think you could convert an int to a string right?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Random Number game C#

Posted 19 July 2016 - 11:32 AM

You're thinking backwards. You get a string from keyboard input. You need to convert that to an int. if you're going to do an numeric comparrison against it. But you have to parse it from string to in. If you try to just cast it like you're doing with the implied int blah = string blahblah then you get the only number .NET knows how to return: The ASCII.

If none of that makes sense... Reread the chapter in your text book or schedule some time with your professor to cover what you missed from class.

And just about anything can have a string representation. Look at how every object has a .ToString() method on it.
Was This Post Helpful? 1
  • +
  • -

#7 Skidkidd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 16

Re: Random Number game C#

Posted 19 July 2016 - 11:57 AM

View PosttlhIn`toq, on 19 July 2016 - 11:32 AM, said:

You're thinking backwards. You get a string from keyboard input. You need to convert that to an int. if you're going to do an numeric comparrison against it. But you have to parse it from string to in. If you try to just cast it like you're doing with the implied int blah = string blahblah then you get the only number .NET knows how to return: The ASCII.

If none of that makes sense... Reread the chapter in your text book or schedule some time with your professor to cover what you missed from class.

And just about anything can have a string representation. Look at how every object has a .ToString() method on it.


BLAH CRAP I CANT BELEIVE I MISSED THAT! Thanks you for the explanation. *facepalm*
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Random Number game C#

Posted 19 July 2016 - 12:49 PM

Something else I want to bring up: Console.Read is weird. I wish more people would just use ReadLine.

First of all, Console.Read does not return a string. It returns an int. The integer value of a char. So you can't do what you mention in one of your posts, where you assign the result of Console.Read to a string and parse it as an integer.

Also, read this:

Quote

The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats


So, put both together, and what you get is the following:

Each call to Console.Read blocks until you hit the <Enter> button. But the value you get back in your variable is just the integer value of the first character you typed. The next call to Console.Read will get the next character. Then the next. Until you hit the end of the buffer, then it blocks and starts over again.

So, have you tried to guess 10 yet? Because you can't with your code, since you're just reading in one digit at a time.




The upshot of all this is, it's simpler to use Console.ReadLine(). That does return a string, and it contains everything you've typed up to the <Enter>.

You can easily write a function to get an integer from the console too:

int GetIntFromConsole(string prompt)
{
	int result;
	string input;
	do
	{
		Console.WriteLine(prompt);
		input = Console.ReadLine();		
	} while (! int.TryParse(input, out result));
	return result;
}


And just as easily call it like this:

int guess = GetIntFromConsole("Guess a number between 1 and 10:");

Was This Post Helpful? 2
  • +
  • -

#9 Skidkidd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 16

Re: Random Number game C#

Posted 19 July 2016 - 01:02 PM

While I was reading you response I was testing the code and I can test 10 and it works.

while (counter > 0)
            {

                Console.Clear();
                Console.WriteLine("You have " + counter + " lives, and " + point + ".");
                Console.ReadLine();
                Console.WriteLine("Guess what number " + number +" I am thinking of between 1 and 10");
                string guessInput = string.Empty;
                guessInput = Console.ReadLine();
                guess = int.Parse(guessInput);

This post has been edited by tlhIn`toq: 19 July 2016 - 01:06 PM
Reason for edit:: No need to quote the entire previous post

Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Random Number game C#

Posted 19 July 2016 - 01:05 PM

Great catch Curtis. I didn't even think to look for .Read instead of .ReadLine. I mean, who would use it. Its documented as not not providing a string or more than one character. Oh yea... students don't read the documentation.

Quote

Return Value
Type: System.Int32

The next character from the input stream,

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1