none
Problems with a C# troubleshooting exercise RRS feed

  • Question

  • DISCLAIMER: Im new to this forum so i may have put this in the wrong place...

    Hi! I have some issues with a task in my beginners course in Programming with C#.

    The task is that we are gonna fix a program with syntax- and logical errors and make it run using TryParse.

    This is a guessing game with int-numbers. If the number is smaller than the random number it enters the first if, if its bigger the 1st else if and if its the same as the random number it exits the application.

    This is my current code:

    using System; // system felstavat
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace Uppgift_4
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool spela = true; // Variabel för att kontrollera om spelet ska fortsätta köras
                Random slumpatTal = new Random(); // skapar ett random-objekt
                int maxValue; //skapar ett maximum int-värde för random-objekt
                maxValue = 20; //deklarerar värde för maxValue
                int spelTal = slumpatTal.Next(maxValue); // anropar Next metoden för att skapa ett slumptal mellan 1 och 20
                int tal;
    
                Console.WriteLine("\n\tGissa på ett tal mellan 1-20");
                while (spela)
    
                    Int32.TryParse(Console.ReadLine(), out tal); // kör tryparse-metoden
    
                if (tal < spelTal)
                {
                    Console.WriteLine("\n\tDet inmatade talet " + tal + " är för litet, försök igen.");
                    Console.ReadLine();
                }
                else if (tal > spelTal)
                {
    
                    Console.WriteLine("\n\tDet inmatade talet " + tal + " är för stort, försök igen.");
                    Console.ReadLine();
                }
                else if (tal == spelTal)
                {
                    Console.WriteLine("\n\tGrattis, du gissade rätt! Det var " + spelTal + "!");
                    Console.WriteLine("\n\tTryck var som helst för att avsluta");
    
                    spela = false;
                }
            }
        }
    }
    
    



    The error i get is

    " Use of unassigned local variable 'tal' "

    My teacher says i should declare the variable outside the while loop, and then locate the input within the while loop. But it doesnt run.... any thoughts on this? Id be very thankful for any help i can get.

    // Hector


    Friday, January 12, 2018 8:53 AM

All replies

  • Hi, if you want to post code, there is a button  to make it much easier to read.

    The error is because you are looking at the variable tal, even though you are not checking whether the earlier call to TryParse() call has worked or not (the syntax checker is pretty clever!).

    You need to check the result of TryParse before using the variable. e.g

    if (Int32.TryParse(Console.ReadLine(), out tal))
    {
       // rest of your code that checks the value of tal
    }

    Friday, January 12, 2018 9:06 AM
  • In addition to RIP1973's recommendation we can declare the variable in the TryParse using C# 7.

    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (Int32.TryParse(Console.ReadLine(), out var tal))
                {
                    // rest of your code that checks the value of tal
                    Console.WriteLine($"tal value is a valid integer: {tal}");
                }
            }
        }
    }
    
    Some might say to not go this route in the event you are not targeting C# 7 but the question should be "why not target C# 7" as it makes life easier to code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 12, 2018 11:20 AM
    Moderator