user2945651
user2945651

Reputation:

Cannot convert type 'String' To 'Int'?

okay so, I have been asked to write a console application for a theater ticket system. A user will type in the number of seats required, and the area of the theater chosen (using the code number 1-4 to represent the seating area chosen) The program should work out and display the cost of the tickets, based on the pricing plan shown below

Area            Code    price
Stalls           1      £24
Grand circle     2      £30
Upper circle     3      £27
Gallery          4      £20

I've so far came up with the following, But it's got an error to do with string + Int conversions under the IF Statements section, this is probably very easy to fix, but I'm new to programming so i'm unsure how to resolve it:

//Declare variables and constants
int iSeatNum;
int iArea;
int iCost;
int iTotalCost;

//Ask the user how many seats they require
Console.WriteLine("How many seats would you like to purchase?");
iSeatNum = Convert.ToInt32(Console.ReadLine());

//Ask the user what area they would like to be in
Console.WriteLine("Where would you like to sit? Please enter 1 for Stalls, 2 for Grand Circle, 3 for Upper Circle or 4 for Gallery");
iArea = Convert.ToInt32(Console.ReadLine());

**if (iArea = "1")**
{
    iCost = 24;
}

//Clarify information & work out
Console.WriteLine("You are buying " + iSeatNum + " Seats at " + iArea);
iTotalCost = iSeatNum * iCost;
Console.WriteLine("Your total ticket cost is " + iTotalCost);

//Prevent from closing
Console.WriteLine("Press any key to close");
Console.ReadKey();

Upvotes: 2

Views: 13156

Answers (5)

poke
poke

Reputation: 387677

if (iArea = "1")

iArea is an integer, "1" is a string. So you cannot compare those two. You should compare with the integer 1 instead. Also note that a single equals symbol (=) is an asignment, and not a comparison. You will want to use two there: ==

if (iArea == 1)

now it displays a further error, when I put iTotalCost = iSeatNum * iCost; it comes up the error of "Use of unassigned local variable iCost" Any idea how I fix this?

The problem is that you declare the variable iCost at the beginning, but never safely assign any value to it before using it. You do assign a value when iArea equals to 1, but for all other cases, the variable remains uninitialized. Of course the compiler doesn’t know that you will end up typing in 1 when the program runs for testing, and that’s not a safe thing anyway. So it requires you to initialize your variable with anything instead.

So at the beginning, you can just say int iCost = 0; to fix this.

Upvotes: 3

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

String strArea =Console.ReadLine();

       if (strArea.Equals("1"))
        {
            iCost = 24;
        }

or

int iArea = Convert.ToInt32(Console.ReadLine());

       if (iArea == 1))
        {
            iCost = 24;
        }

Upvotes: 0

Irvin Dominin
Irvin Dominin

Reputation: 30993

Because you have already converted you string (the Console.ReadLine() return a string) into number using:

iArea = Convert.ToInt32(Console.ReadLine());

you can compare it as number using:

if (iArea == 1)

note the == instead of =, the single is used for assignment, the double for comparison.

Upvotes: 1

Trevor Elliott
Trevor Elliott

Reputation: 11252

if (iArea = "1")

This doesn't make sense. First of all you're using the assignment equals operator. You're attempting to assign iArea the value of "1". Instead, you need the logical equality operator == which will return true or false depending on whether the first operand is equal to the second operand.

Second, you have already converted the string value read from the console to a strongly typed integer. So you need to write your if statement as follows:

if (iArea == 1)

Upvotes: 0

Arran
Arran

Reputation: 25056

Well "1" is a string, not int.

if (iArea == 1)

Upvotes: 1

Related Questions