Click here to Skip to main content
15,887,911 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote this program and asks a user to guess a number between 20 and 30 and to avoid the user entering something like a letter, I used a certain code but I would like to know how to use the tryParse method. Could someone please put the tryParse method in my code?

C#
using System;

class ApplicationEntry
{
    static void Main(string[] args)
    {
        bool repeat = true, repeat2 = true, repeat3 = true;
        while (repeat == true)
        {
            repeat2 = true;
            Random r = new Random();
            int numb = (r.Next(20, 30));
            while (repeat2 == true)
            {
                Console.WriteLine("Please enter a numbers between 20 and 30");
                int guess1 = Convert.ToInt32(Console.ReadLine());

                if ((guess1 < 20 || guess1 > 30) && guess1 != numb)
                {
                    Console.WriteLine("Invalid. Please enter a number between 20 and 30");
                    repeat3 = false;
                }
                else if (guess1 == numb)
                {
                    Console.WriteLine("Good job");
                    repeat2 = false;
                    repeat3 = true;
                }
                else
                {
                    Console.WriteLine("Wrong! Try again");
                    repeat2 = true;
                    repeat3 = false;
                }
                while (repeat3 == true)
                {
                    Console.WriteLine("Would you like to play again?");
                    string answer = Console.ReadLine();
                    if (answer == "yes" || answer == "Yes" || answer == "YES")
                    {
                        repeat3 = false;
                        repeat = true;
                        repeat2 = false;
                    }
                    else if (answer == "no" || answer == "No" || answer == "NO")
                    {
                        repeat = false;
                        repeat3 = false;
                    }
                    else
                    {
                        Console.WriteLine("I didn't understand that");
                    }
                    

                 }

             }
          }

            Console.Write("\nPress any key to exit program: ");
            Console.ReadKey();
     } 

}
Posted
Updated 18-Mar-13 22:25pm
v4
Comments
[no name] 18-Mar-13 22:23pm    
Which part of the documentation were you unable to understand? There is a perfectly good example right there, http://msdn.microsoft.com/en-us/library/f02979c7.aspx
[no name] 18-Mar-13 22:42pm    
You are seriously having a hard time figuring out?
int number;
bool result = Int32.TryParse(yourstring, out number);?
What is so unclear about it?
Kishor Deshpande 19-Mar-13 4:26am    
It's a repost!!!
Yrtsg 19-Mar-13 4:34am    
this was actually the first one. the other was a repost because i got no help in this one

May I recommend a NumericUpDown?
 
Share this answer
 
You have posted your question two times, BTW, I have already posted solution at How do I use the tryParse method in my program?[^]
 
Share this answer
 
Comments
Yrtsg 19-Mar-13 4:33am    
this was the first time i posted this. the reason was because this guy kept asking me what's wrong me with me and why i cant understand it instead of actually helping. when i posted it the second time you guys helped so thank you

I sort of understand it but i'm not sure how to implement it into my code
 
Share this answer
 
v2
where does the string come from?
 
Share this answer
 
Comments
[no name] 18-Mar-13 22:50pm    
Why are you posting this as a solution? What do you mean where does the string come from? Are you or are you not getting a string from the user?
Yrtsg 18-Mar-13 23:20pm    
sorry I'm new to this forum. The program generates a random number and the user has to guess it. The guess is an int

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900