0

I am trying to write a program that simulates a car race competition, the user inserts the number of cars in the competition and the time of each car. The program will print the car with the fastest time and the car with the second fastest time.

So I wrote this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int numc, first, second, time, i, temp;
            Console.WriteLine("Please insert the number of cars in the competition");
            numc = int.Parse(Console.ReadLine());
            Console.WriteLine("Please insert the time it took the car to finish the race");
            time = int.Parse(Console.ReadLine());
            first = time;
            for (i = 0; i < numc; i++)
            {
                Console.WriteLine("Please insert the time it took the car to finish the race");
                time = int.Parse(Console.ReadLine());
                if(time<first)
                {
                    temp=first;
                    first = time;
                    second = temp;
                }
            }
            Console.WriteLine("The time of the car who got first place is:" +first);
            Console.WriteLine("The time of the car who got second place is:" +second);
            Console.ReadLine();
        }
    }
}

I get this error:

Use of unassigned local variable 'second'

I don't understand why do I get this error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Amnon Hanuhov
  • 21
  • 1
  • 3
  • 9

4 Answers4

2

You're only assigning second inside this loop:

if(time<first)
{
     temp=first;
     first = time;
     second = temp;
}

What happens if you don't get inside this if?

You have to make sure it is assigned no matter where it goes if you want to use it later on.

Omri Aharon
  • 16,959
  • 5
  • 40
  • 58
1

You declare the variable:

int numc, first, second, time, i, temp;

Then you might assign it:

for (i = 0; i < numc; i++)
{
    // etc.
    if(time<first)
    {
        temp=first;
        first = time;
        second = temp;
    }
    // etc.
}

(or you might not, depending on that condition at runtime or the value of numc at runtime.)

Then you use it:

Console.WriteLine("The time of the car who got second place is:" +second);

What happens if that if condition evaluates to false? Or if the for loop doesn't iterate over anything? Then the variable is never assigned before you use it. That's what the compiler is telling you.

If you're going to always use the variable then you need to make sure that you always assign some value to it.

David
  • 208,112
  • 36
  • 198
  • 279
1

The problem here is that your assignment

second = temp

will not have executed if numc is entered as less than one.

Since the compiler cannot therefore guarantee that it has been assigned, it gives you the warning.

In your case, you could do something like assigning

int second = 0;

but you probably want to change the Console.WriteLine bit too, to something like:

if (numc > 0)
{
    Console.WriteLine("The time of the car who got first place is:" +first);
    Console.WriteLine("The time of the car who got second place is:" +second);
}
else
{
    Console.WriteLine("No cars were in the competition");
}

Console.ReadLine();
Rob Levine
  • 40,328
  • 13
  • 85
  • 111
0

This line:

Console.WriteLine("The time of the car who got second place is:" +second);

uses the second variable which is unassigned, when numc < 1 or time >= first.

Use

int second = 0;

to initialize this field.

romanoza
  • 4,775
  • 3
  • 27
  • 44