none
Odd problem with variables in a simple program. RRS feed

  • Question

  • {
        class Program
        {
            static void Main(string[] args)
            {
                string name;
                double firstnumber, secondnumber;
                Console.WriteLine ("Hello User");
                Console.WriteLine ("Please Enter Name");
                name = Console.ReadLine();
                Console.WriteLine("Hi {0}, welcome to this program.", name);
                Console.WriteLine("Please give me a number:");
                try
                { firstnumber = Convert.ToDouble(Console.ReadLine()); }
                catch
                { Console.WriteLine("I SAID, GIVE ME A NUMBER"); }
                Console.WriteLine("Okay, now give me another:");
                try
                { secondnumber = Convert.ToDouble(Console.ReadLine()); }
                catch
                { Console.WriteLine("I SAID, GIVE ME A NUMBER"); }
                Console.WriteLine("Well, for starters, {0} plus {1} plus {2}", firstnumber, secondnumber, firstnumber + secondnumber);
                Console.ReadKey();
    
            }
        }
    }
    I'm having a weird problem with my C# code. If you could tell me what I am doing wrong, I would appreciate it.
    I am just learning the languge, so it is probably something easy and quite simple, I am just stumped. Even though I declared the variables and then assigned them later on, with Console.ReadLine, it shows the error of "Error 1 Use of unassigned local variable 'firstnumber' 28 76 CH4Ex0001"
    and the same error for the secondnumber variable. I think the problem is in how I handled the FormatException, using try and catch.


    Any help would be greatly apprecited.
    Wednesday, May 20, 2009 3:52 PM

Answers

  • you still dint get it... If the user types something unusual (a value that cant be converted to double), then the Console.WriteLine will try to write an uninitialized variable. VS is pointing it out that.

    Better initialize the firstnumber and secondnumber with some values.
    Mark as answers if you find them helpful //Guna
    • Marked as answer by monkoosbob0 Wednesday, May 20, 2009 4:17 PM
    Wednesday, May 20, 2009 4:15 PM
  • Also don't have excpetions handle known events, plan for them in code and have the execptions either halt operations or report what is the problem and restructure code. Here is an example of handling if the user inputs bad data and the exception will tell you what to program against in future generations of this code:

    string value;
    Double dm;
    try
    {
        do
        {
            value = Console.ReadLine();
    
        } while ( string.IsNullOrEmpty( value ) || // Exit if nothing is type in. 
                  ( double.TryParse( value, out dm ) == false ) );
    
    }
    catch ( Exception ex ) 
    {
        Console.WriteLine( "Unknown Exception caught :" + ex.Message );
    }


    HTH GL
    William Wegerson (www.OmegaCoder.Com )
    • Marked as answer by monkoosbob0 Wednesday, May 20, 2009 5:01 PM
    Wednesday, May 20, 2009 4:22 PM
    Moderator

All replies

  • Try to answer this, what will the value of firstnumber be if the user types in "FOO"?
    /* No comments */
    Wednesday, May 20, 2009 3:57 PM
  • It would give me an exception, which is why I tried to use at least the beginnings of exception handling, though I was quite unsuccessful.
    Wednesday, May 20, 2009 4:03 PM
  • But that isn't even my main problem. Right now, VS has underlined this line
    Console.WriteLine("Well, for starters, {0} plus {1} plus {2}", firstnumber, secondnumber, firstnumber + secondnumber); 
    well, at least the firstnumber, secondnumber part of it. Which makes it really hard to debug and/or write more code for it.
    Wednesday, May 20, 2009 4:08 PM
  • you still dint get it... If the user types something unusual (a value that cant be converted to double), then the Console.WriteLine will try to write an uninitialized variable. VS is pointing it out that.

    Better initialize the firstnumber and secondnumber with some values.
    Mark as answers if you find them helpful //Guna
    • Marked as answer by monkoosbob0 Wednesday, May 20, 2009 4:17 PM
    Wednesday, May 20, 2009 4:15 PM
  • Ahhhh, I get it now, thank you.
    Wednesday, May 20, 2009 4:17 PM
  • Also don't have excpetions handle known events, plan for them in code and have the execptions either halt operations or report what is the problem and restructure code. Here is an example of handling if the user inputs bad data and the exception will tell you what to program against in future generations of this code:

    string value;
    Double dm;
    try
    {
        do
        {
            value = Console.ReadLine();
    
        } while ( string.IsNullOrEmpty( value ) || // Exit if nothing is type in. 
                  ( double.TryParse( value, out dm ) == false ) );
    
    }
    catch ( Exception ex ) 
    {
        Console.WriteLine( "Unknown Exception caught :" + ex.Message );
    }


    HTH GL
    William Wegerson (www.OmegaCoder.Com )
    • Marked as answer by monkoosbob0 Wednesday, May 20, 2009 5:01 PM
    Wednesday, May 20, 2009 4:22 PM
    Moderator