-2

The variable area in line 18 keeps giving me the "Use of unassigned variable" error. I've tried putting area = length*width in the int line but that hasn't worked either it just makes length and width unassigned. :/ This is probably me just being really bad but help

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

namespace Room
{
class Program
{
    public static void Main(string[] args)
    {
        string AreaString;
        int length;
        int width;
        int area;
        double persquarefoot = 3.25;
        double priceofcarpet = area * persquarefoot;
        Console.Write("Enter the length of the room: ");
        AreaString = Console.ReadLine();
        length = Convert.ToInt32(AreaString);
        Console.Write("Enter the width of the room: ");
        AreaString = Console.ReadLine();
        width = Convert.ToInt32(AreaString);
        area = length * width;
        Console.Write("The floor space is");
        Console.Write(area);
        Console.WriteLine(" square feet.");
        Console.Write("The cost of carpet in the room is $");
        Console.Write(persquarefoot);
        Console.WriteLine(" per square foot.");
        Console.Write("The cost of carpeting the whole room would be $");
        Console.Write(priceofcarpet);
        Console.WriteLine(".");
        Console.ReadKey();
    }
}
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Sol
  • 11
  • 2
  • Even if that were allowed, the `priceofcarpet` would always be zero so it doesn't make sense to use the default value of an int in a multiplication expression. – Crowcoder Oct 03 '18 at 12:03
  • 1
    What would you expect `double priceofcarpet = area * persquarefoot;` to return when you never said what `area`? Compiler won´t make any guesses on what may be a good default-value for `area`. – MakePeaceGreatAgain Oct 03 '18 at 12:04
  • You can't use area without first giving it a value, but that will be difficult for you because you are trying to use area before you even asked what the length and width are. – takendarkk Oct 03 '18 at 12:04

1 Answers1

1

You should move below snippet

double priceofcarpet = area * persquarefoot;

after calculating area

area = length * width;
Abhijit
  • 374
  • 3
  • 15
Milad Aghamohammadi
  • 1,866
  • 1
  • 16
  • 29