C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-15-2008, 10:21 AM   #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
psyadam is offline   Reply With Quote
Old 07-15-2008, 10:28 AM   #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);
            }
        }
    }
}
psyadam is offline   Reply With Quote
Old 07-15-2008, 10:34 AM   #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?
psyadam is offline   Reply With Quote
Old 07-15-2008, 12:12 PM   #4
Registered User
 
Join Date: Jun 2003
Posts: 90
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
DanFraser is offline   Reply With Quote
Old 07-15-2008, 12:43 PM   #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.
psyadam is offline   Reply With Quote
Old 07-15-2008, 12:48 PM   #6
Registered User
 
Join Date: Jun 2003
Posts: 90
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
DanFraser is offline   Reply With Quote
Old 07-15-2008, 12:52 PM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-22-2008, 08:09 PM   #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.
Azimuth is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
memory leak aruna1 C++ Programming 3 08-17-2008 10:28 PM
Function argument assignment between types "unsigned int*" and "unsigned long*" nadeer78 C Programming 8 03-10-2008 11:57 AM
Working with random like dice SebastionV3 C++ Programming 10 05-26-2006 09:16 PM
getting a headache sreetvert83 C++ Programming 41 09-30-2005 05:20 AM
How do you search & sort an array? sketchit C Programming 30 11-03-2001 05:26 PM


All times are GMT -6. The time now is 11:37 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22