Jump to content

Faulty C# code

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
gwudou

gwudou

    Newbie

  • Members
  • Pip
  • 2 posts
hello

I'm beginning with C# and I am using a book and here the code they give me.

I checked the syntaxe and it still not working...

Here is the code, can somebody tell me where the mistake is ???? please

using System;



public class Guess
{
  private static int getRandomNumber ( int nbr )
    {
        if (nbr > 0)
        {
           System.Random rnd = new System.Random();
            return (rnd.Next(0, nbr));
        }
        else
        {
            return 0;
        }
  }

    private static void WriteStats(string Guess, int nbr, string err)
    {
        Console.WriteLine("\nbr=================================");
        Console.WriteLine("Current Guess: {0}", Guess);
        if (err != "")
            Console.WriteLine("Enter a number from 1 to 10000");
        Console.WriteLine("==================================");

        return;
    }

    public static void Main(string[] args)
    {
        int WinningNumber = Guess.getRandomNumber(10000);
        int Guesses = 0;
        string Curr = "";
        int val = 0;
        string errMsg;

        bool cont = true;

        WriteStats(Curr, Guesses, (string)"");


        while (cont == true)
        {
            Console.Write("\nEnter Guess: ");
            Curr = Console.ReadLine();

            try  // try, catch and finally are coverd on Day 9
            {
                val = Convert.ToInt32(Curr);

                Guesses += 1;

                if (val < 0 || val > 10000)
                {
                    errMsg = "Number is out of range... Try again";
                    WriteStats(Curr, Guesses, errMsg);
                }
                else
                {
                    if (val < WinningNumber)
                    {
                        errMsg = "You guessed too low... Try again";
                        WriteStats(Curr, Guesses, errMsg);
                    }
                    else
                   
                        if (val > WinningNumber)
                        {
                            errMsg = "You guessed too high... Try again";
                            WriteStats(Curr, Guesses, errMsg);
                        }
                        else
                        {
                            Console.WriteLine("\nCurrent guess: {0}\n", val);
                            Console.WriteLine("Number of Guesses: {0}\n", Guesses);
                            Console.WriteLine("You guessed correctly!");
                            cont = false;
                        }
                }
            }
            catch (FormatException)
            {
                errMsg = "Please enter a valid number...";
                WriteStats(Curr, Guesses, errMsg);
            }
        }
    }
}

Edited by ZekeDragon, 05 March 2010 - 07:58 AM.
Read cdg10620's first statement, remember to use [code] tags.


#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
1. Please use [CODE] tags around your code.
2. Did you compile this and get an error? If so, did the error tell you a line number? What was the error?
3. What are you using to compile this code?
4. Are you sure that you copied every bracket and semi-colon? If you miss one little thing it can cause the code to break.
5. C# is a case sensitive language. Did you make sure that you copied exactly when you copied the code?

Check these things. Let me know if you still have issues. if you do then please post the exact error message.
-CDG10620
Software Developer

#3
lobo521

lobo521

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
Iserted your code to vs and everything works.