Thread: random to int?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    random to int?

    Hi I'm trying to teach myself C# to make myself more marketable as a programmer. So far all I know is C++.

    Anyway, a friend advised me just to start writing simple C# programs and then eventually I'll "know" C#. (You know what I'm talking about)

    My first simple program I'm trying to write is a guessing game where the computer thinks of a random number between 1 and 100 and the human tries to guess it. Here's my code so far:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Guessing_Game
    {
        class Program
        {
            static void Main(string[] args)
            {
                string guess;
                Console.WriteLine("Welcome to Adam Henderson's C# Guessing Game!\n");
                Console.WriteLine("Guess a number from 1 to 100.");
                guess = Console.ReadLine();
               
                Random random = new Random(); // C#'s way of giving you a random number
                random.Next(1, 100);
    
                int guess2 = Int32.Parse(guess);
                if (random < guess2)
                    Console.WriteLine("Oh!  You guessed too high!  Guess again!");
                else if (random > guess2)
                    Console.WriteLine("Oh!  You guessed too low!  Guess again!");
                else
                {
                    Console.WriteLine("You guessed correct!  The answer was {0}!", guess2);
                }
            }
        }
    }
    Problem is that when I try to compare a variable of type int to a variable of type Random I get an error. What can I do? Thanks, ~Adam

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    I think I fixed it *blush*:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Guessing_Game
    {
        class Program
        {
            static void Main(string[] args)
            {
                string guess;
                Console.WriteLine("Welcome to Adam Henderson's C# Guessing Game!\n");
                Console.WriteLine("Guess a number from 1 to 100.");
                guess = Console.ReadLine();
               
                Random random = new Random(); // C#'s way of giving you a random number
                int rand = random.Next(1, 100);
    
                int guess2 = Int32.Parse(guess);
                if (rand < guess2)
                    Console.WriteLine("Oh!  You guessed too high!  Guess again!");
                else if (rand > guess2)
                    Console.WriteLine("Oh!  You guessed too low!  Guess again!");
                else
                {
                    Console.WriteLine("You guessed correct!  The answer was {0}!", guess2);
                }
            }
        }
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    Looks like I was able to complete the random number guessing game in C#. Wow, I'm spent! (Just kidding!)

    Anyone have an idea for the next small program I should try to make?

    edit: oh yeah, I noticed that I used the word "new" when I made the random number. I think my friend said that C# has a garbage collector. Does this mean I can use new without having to worry about memory leaks?

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    I'll give you an assignment I got in my first semester at Uni...

    Create a program that asks a user to answer 10 math questions. The questions should just be a simple +, -, / or x question. When the user has finished, output the unmber of questions answered correctly and give a score. At the end, ask if they'd like to go again.

    Suggestion for layout and a nice example:

    Code:
    9: What is 4 x 3?  12
    Correct!
    
    10: What is 7-2? 3
    Wrong!
    
    Your total score was 6/10
    
    Would you like to play again? (imagine blinking cursor here!)
    Nice and simple, teaches you loops, randoms, and program layout for a beginner
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    Quote Originally Posted by DanFraser View Post
    I'll give you an assignment I got in my first semester at Uni...

    Create a program that asks a user to answer 10 math questions. The questions should just be a simple +, -, / or x question. When the user has finished, output the unmber of questions answered correctly and give a score. At the end, ask if they'd like to go again.

    Suggestion for layout and a nice example:

    Code:
    9: What is 4 x 3?  12
    Correct!
    
    10: What is 7-2? 3
    Wrong!
    
    Your total score was 6/10
    
    Would you like to play again? (imagine blinking cursor here!)
    Nice and simple, teaches you loops, randoms, and program layout for a beginner
    I need a more challenging assignment than that. That is far too easy. I've done C++ for years, I'm just trying to learn C# to the point that I can say that I can program in it to perspective employers. So I'm looking for things that would help me learn C# specific concepts, or things that are in C++ but I may not have used but are vital in C#. Like conversions, boxing/unboxing, advanced function stuff, how do pointers work in C#?, delegates (whatever that is), whatever else you can think of.

    edit: windows programming assignments would be very helpful as well

    edit: thinking more along the lines of pointers, writing a class for a singly linked list sounds like a good exercise
    Last edited by psyadam; 07-15-2008 at 12:46 PM.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Sorry, I was looking at the code you were working on and just stepped that up a little

    As you're experienced with C++, I think you'll find the C# specification very easy to read and skim through! Boxing and unboxing is a thing where you learn what it does, then avoid doing it. An example is an ArrayList of a class you've made. It'll get stored as an object type in the arraylist, and you have to convert it out to it's original if you want to use it, this is where generics come in very handy. Advanced function stuff to me is pretty much the same as C++ from what I can see (not personally done that much C++ myself, but I can do it). Delegates, I've no clue either!
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I understand correctly, C# does not believe in pointers. So that may make life a little tricky.

    Maybe dealing with generics? You should never write a singly linked list in C++ (unless you're taking data structures in school, or for other abstruse purposes), and I'm guessing you should never write a singly linked list in C# either.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    9
    C# does have pointers, but they should be placed in unsafe blocks and compiled with a special flag.
    Delegates are C#'s rough equivalent to C/C++ function pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM