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.