I took the liberty of posting the assignment and the work I've done. I can't figure this one out, can anyone help me
And I did put an attempt at this, almost 4 hours now and I'm burnt out. If anyone can help me I would soooo love you.

"Your app should sell tickets until the plane is full. For each customer, ask how many seats he wants to buy. If the plane has enough seats available, ask the customer to enter the name of each passenger. If the plane does not have enough seats, tell the customer how many seats are available (please see the example below).

``````When the plane is full, display the names of all passengers.

[Hint: Use a string array to store the names of the passengers.  Create a variable to store the number of tickets sold.  That will help you to decide where in the array to store each passenger’s name.]
``````

How many seats do you need? 5
Name: Peter Chen
Name: Frank Chao
Name: Al Molin
Name: Walter Rotenberry
Name: Hong Cui

How many seats do you need? 3
Name: Mary Orazem
Name: Hillary Paul

How many seats do you need? 4
Sorry! Only 2 seats left

How many seats do you need? 2
Name: Cindy Foster
Name: Man-Chi Leung

All seats are sold.
List of passengers:
Peter Chen
Frank Chao
Al Molin
Walter Rotenberry
Hong Cui
Mary Orazem
Hillary Paul
Cindy Foster
Man-Chi Leung
Press any key to continue . . .

``````using System;

public class Airline
{
public static void Main(string[] args)
{
int total = 10;
string[] passengers = new string[10];
int ticketsSold = 0;

{
Console.WriteLine("How many seats do you need? ");

if (ticketsSold <= total)
{

for (int i = answer; i < ticketsSold; i++)
{
Console.Write("Name: ");
}

total -= ticketsSold;
}
else if (ticketsSold > passengers.Length)
{
Console.WriteLine("Sorry only {0} seats left", total);
}

else if (ticketsSold == passengers.Length)
{
Console.WriteLine("All seats are sold\nList of passengers:\n");
for (int x = 0; x < passengers.Length; x++)
{
Console.WriteLine("{0}", passengers[x]);
}
}

}
} // end Main
} // end class Airline
``````

i have a question.. why do you used C# for this? you can use c++ for Console aplication, i think that is much simpler for this kind of programs..

it's for my class

You probably want to do some input validation to ensure that the user entered an integer value when an integer value is required. See "isInt" below for a way of performing integer validation.

Declare the following variables as global (before "Main"):

``````        //need to declare as static
//if used inside static methods

//passenger array
private static string[] passengers;

//number of tickets sold
private static int ticketsSold = 0;

//number of seats remaining
private static int seatsRemaining = 0;
``````

Main:

``````            //for prompting to exit

//clear the screen
Console.Clear();

//write empty line
Console.WriteLine();

//total airplane seats
//string variable is for console input
//int variable is for use in our program
int totalNumberOfSeatsInt = 0;
string totalNumberOfSeatsStr = string.Empty;

//total airplane seats user is trying to purchase
//string variable is for console input
//int variable is for use in our program
int ticketsToPurchaseInt = 0;
string ticketsToPurchaseStr = string.Empty;

Boolean isValidInput = false;

//--------------------------
//get number of seats
//--------------------------

//loop until we receive an int
//if number of seats available = 0, exit
do
{
//prompt user
Console.Write("How many seats are on the airplane? ");

//get number of seats being purchased

//check to see if input was an integer
isValidInput = isInt(totalNumberOfSeatsStr);

//if input is an integer, convert to int
if (isValidInput == true)
{
totalNumberOfSeatsInt = Convert.ToInt32(totalNumberOfSeatsStr);

//if no seats are available, exit
if (totalNumberOfSeatsInt <= 0)
{
Console.WriteLine("Exiting. Thank you.");
Console.WriteLine();
return; //exit
}//if
}//if
else
{
//write empty line
Console.WriteLine();

Console.WriteLine("Error: Invalid input. Please enter an integer.");

//write empty line
Console.WriteLine();
}//else

} while (isValidInput == false);

//size our array based on number
//of seats available
passengers = new string[totalNumberOfSeatsInt];

//set seatsRemaining to the number
//of available seats on the airplane
seatsRemaining = totalNumberOfSeatsInt;

//--------------------------
//sell tickets
//--------------------------

//loop until we receive an int
//if number of seats available = 0, exit
do
{
//write empty line
Console.WriteLine();

//prompt user
Console.Write("How many seats do you need? ");

//get number of seats being purchased

//check to see if input was an integer
isValidInput = isInt(ticketsToPurchaseStr);

//if input is an integer, convert to int
if (isValidInput == true)
{
ticketsToPurchaseInt = Convert.ToInt32(ticketsToPurchaseStr);

//if user entered 0,
//prompt to exit
if (ticketsToPurchaseInt == 0)
{

//write empty line
Console.WriteLine();

Console.Write("You entered 0 seats. ");

do
{
//write empty line
Console.WriteLine();

Console.Write("Are you sure you want to exit (y or n)? ");
} while (string.Compare(answer, "y", true) != 0 && string.Compare(answer, "n", true) != 0);

if (string.Compare(answer, "y", true) == 0)
{
//write empty line
Console.WriteLine();

Console.WriteLine("Exiting. Thank you for your interest.");

//write empty line
Console.WriteLine();

break; //exit while loop
}//if
else
{
//set isValidInput = false
//so we loop again
isValidInput = false;
}//else
}//if
else if (ticketsToPurchaseInt > seatsRemaining)
{
//write empty line
Console.WriteLine();

if (seatsRemaining > 1)
{
Console.WriteLine("Sorry only {0} seats left.", seatsRemaining);
}//if
else
{
Console.WriteLine("Sorry only {0} seat left.", seatsRemaining);
}//else

//write empty line
Console.WriteLine();

do
{
Console.Write("Would you like to purchase the remaining seats available? (y or n): ");
} while (string.Compare(answer, "y", true) != 0 && string.Compare(answer, "n", true) != 0);

if (string.Compare(answer, "y", true) == 0)
{
//set tickets to be purchased = seatsRemaining
ticketsToPurchaseInt = seatsRemaining;

purchaseTicket(ticketsToPurchaseInt);
}//if
else
{
continue; //go to next loop
}//else
}//else if
else if (ticketsToPurchaseInt <= seatsRemaining)
{
purchaseTicket(ticketsToPurchaseInt);
}//else if
}//if
else
{
//write empty line
Console.WriteLine();

Console.WriteLine("Error: Invalid input. Please enter an integer.");

//write empty line
Console.WriteLine();
}//else

} while ((ticketsToPurchaseInt > 0 && seatsRemaining > 0) || isValidInput == false);

//write empty line
Console.WriteLine();

if (ticketsSold == passengers.Length)
{
Console.WriteLine("All seats are sold.");
}//if

Console.WriteLine("List of passengers:\n");

for (int x = 0; x < passengers.Length; x++)
{
//only print non-empty ones
if (!string.IsNullOrEmpty(passengers[x]))
{
Console.WriteLine("{0}", passengers[x]);
}//if
}//for

//write empty line
Console.WriteLine();

Console.WriteLine("Press any key to continue...");

``````

isInt:

``````        private static Boolean isInt(string data)
{
int dataInt = 0;

//Int32.TryParse
//returns true if string can
//be converted to an Int32.
//otherwise, it returns false
return Int32.TryParse(data, out dataInt);

}//isInt
``````

purchaseTicket

``````        private static void purchaseTicket(int numberOfTickets)
{
//start after the last index inserted
//last ticket sold is in ticketsSold - 1
int index = ticketsSold;
string name = string.Empty;

//write empty line
Console.WriteLine();

for (int i = 0; i < numberOfTickets; i++)
{
//prompt until text is entered
do
{
Console.Write("Name #{0}: ", i+1);

//add passenger name to passengers array
passengers[index] = name;

} while (String.IsNullOrEmpty(name));

//increment index
index += 1;

//update seatsRemaining
seatsRemaining -= 1;

//update ticketsSold
ticketsSold += 1;

//Console.WriteLine("Tickets sold: {0} Seats remaining: {1}", ticketsSold, seatsRemaining);
}//for

}//purchaseTicket
``````

Note: The above will print the passenger list regardless of whether or not all of the seats have been sold. However, it keeps prompting to sell seats until all of the seats have been sold or the user chooses to quit.

If you wanted to allow the user to choose a seat (row, column), you could use a 2-dimensional array.

your code is mostly correct. You have the limit on the first for loop as less than or equals(<=) to passengers.Length. This means you must oversell 1 seat in order to exit. This should be just less than(<). Also the routine to print the names should be outside the main loop. Here's the code with revisions:

``````    public static void Main(string[] args)
{
int total = 10;
string[] passengers = new string[10];
int ticketsSold = 0;
{
Console.WriteLine("How many seats do you need? ");
if(ticketsSold <= total)
{
for(int i = answer; i < ticketsSold; i++)
{
Console.Write("Name: ");
}
total -= ticketsSold;
}
else if(ticketsSold > total)
{
Console.WriteLine("Sorry only {0} seats left", total);
}
else if(ticketsSold == passengers.Length)
{
break;
}
}
Console.WriteLine("All seats are sold\nList of passengers:\n");
for(int x = 0; x < passengers.Length; x++)
{
Console.WriteLine("{0}", passengers[x]);
}
}
``````

While this works as required, there is one improvement that should be made. For converting the Console.ReadLine to int use the int.TryParse method instead this allows you to validate the user's input before proceeding.

In general anytime you ask the user for input you should have some way of validating it before proceeding.

tinstaafl - Thanks that worked out, but there was a problem. I inputted 6 the first time and then entered their names, then I entered 5 and the error came up which is what i wanted; however, when i inputted the rest of the 4 names it did not display the remaining four at the end. It displayed the names of the 4 inputted and overwrited the last 2 with the fifth and sixth name of the original names first inputted.

I think the problem is where its storing the data. When I input the first 6 names this code runs

``````for (int i = 0; i < ticketsSold; i++)
{
Console.Write("Name: ");
}
``````

However when it asks for the remaining 4 it starts back at 0 and stores the name in passengers[0] - passengers[4] is there a way to have it where it starts where it finishes storing from the last amount of data

In my above code I tried substituting `int i = 0` with `int i = answer` but no luck

You were on the right track. You need to add `i` to `answer` for the index of the array:

``````for(int i = 0; i < ticketsSold; i++)
{
Console.Write("Name: ");
}
``````

Here's the complete routine:

``````public static void Main(string[] args)
{
int total = 10;
string[] passengers = new string[10];
int ticketsSold = 0;
{
ticketsSold = 0;
while(ticketsSold == 0)
{
Console.WriteLine("How many seats do you need? ");
//If the TryParse fails to convert ReadLine to an int
//ticketsSold will be unchanged and TryParse will
//return false.  The error message will display and
//the while loop will loop again
//If it succeeds, ticketsSold will contain the converted
//value and the while loop will exit
Console.WriteLine("Invalid entry - whole numbers only");
}
if(ticketsSold <= total)
{
for(int i = 0; i < ticketsSold; i++)
{
Console.Write("Name: ");
}
total -= ticketsSold;
}
else if(ticketsSold > total)
{
Console.WriteLine("Sorry only {0} seats left", total);
}
else if(ticketsSold == passengers.Length)
{
break;
}
}
Console.WriteLine("All seats are sold\nList of passengers:\n");
for(int x = 0; x < passengers.Length; x++)
{
Console.WriteLine("{0}", passengers[x]);
}