New to C# (only coding for a week so far) trying to create a practice program. Can't seem to get the data I want stored in 'price1' and 'price2'. Error is CS0165 Use of unassigned local variable 'price1' and 'price2'.
I've tried moving lines of code around and adding in a return command, but I can't quite seem to figure it out.
Console.Write("What grocery are you buying: ");
string product1 = Console.ReadLine();
Console.Write("How many are you buying: ");
int quantity1 = Convert.ToInt32(Console.ReadLine());
double price1;
if (product1 == "Steak")
{
price1 = Convert.ToDouble(steak.price * quantity1);
}
if (product1 == "Cheerios")
{
price1 = Convert.ToDouble(cheerios.price * quantity1);
}
if (product1 == "Pepsi")
{
price1 = Convert.ToDouble(pepsi.price * quantity1);
}
if (product1 == "Celeste Pizza")
{
price1 = Convert.ToDouble(celeste.price * quantity1);
}
Console.Write("What second grocery are you buying: ");
string product2 = Console.ReadLine();
Console.Write("How many are you buying: ");
int quantity2 = Convert.ToInt32(Console.ReadLine());
double price2;
if (product2 == "Steak")
{
price2 = Convert.ToDouble(steak.price * quantity2);
}
if (product1 == "Cheerios")
{
price2 = Convert.ToDouble(cheerios.price * quantity2);
}
if (product1 == "Pepsi")
{
price2 = Convert.ToDouble(pepsi.price * quantity2);
}
if (product1 == "Celeste Pizza")
{
price2 = Convert.ToDouble(celeste.price * quantity2);
}
Console.WriteLine(price1 + price2);
Trying to get data stored in 'price1' and 'price2' so I can add them together at the end. Sorry if I'm getting any terminology wrong here.