Credit Card | Unsecured Loans | Fuente De | Cell Phones | Loans
C++ program help [Archive] - PCMech Forums

PDA

View Full Version : C++ program help


tominator
12-09-2004, 10:06 PM
for this program i have to design an airline reservation program. i need to use a single array to reserve the seats. there are 10 seats available. 0-4 are first class, and 5-9 are economy. if one seat gets reserved the program should place a 1 on the seat (array) number...then it prints a voucher telling the seat number and if thier in first class or economy. the program shouldnt allow a reservation for a seat thats taken, it should print seat it taken. and when all the seats are filled up.....program should print next flight is in three hours.....im having trouble with this...heres what i did so far....can anyone help?
this is what i have so far.....

#include <iostream>

int seats[ 9 ];

int main()
{

int choice;
int count;

std::cout << "Welcome to the airline reservations system.\n";
std::cout << "Please type 1 for First Class or type 2 for Economy.\n";
std::cin >> choice;

if ( choice == 1 )
{
seats[ 0 ] = 1;
std::cout << "Your are seat Number:\n" ;
std::cout << "Your seat is in FIRST CLASS, have a nice flight.\n";
}

if ( choice == 2 )
{
seats[ 5 ] = 1;
std::cout << "You are seat number:\n" ;
std::cout << "Your seat is in the ECONOMY SECTION, have a nice flight.\n";
}

return 0;
}

shaticus
12-14-2004, 08:31 AM
Your going to need a some sort of loop for it(while,for). what loop depends on if you have any style guidelines for the program. You will also have to check if the seat is taken before putting anyone in it(if statement).

#include

int seats[ 9 ];

int main()
{

int choice;
int count;
int i = 0;
std::cout << "Welcome to the airline reservations system.\n";
while(seats[i] > 0 && seats[i] < 9) // checks to make sure there is an open seat
if(seats[i] == 9) // if all seats are full display this message else continue execution.
std::cout <<"Next flight leaves in three hours.\n";
else
{
std::cout << "Please type 1 for First Class or type 2 for Economy.\n";
std::cin >> choice;

if ( choice == 1 )
{
i = 0;
while(seats[i] > 0 && i < 5)
i++;
if(seats[i] == 5)
std::cout << " Sorry, All first class seats are full.\n";
else
{
seats[ i ] = 1;
std::cout << "Your are seat Number:\n" ;
std::cout << "Your seat is in FIRST CLASS, have a nice flight.\n";
}
}

if ( choice == 2 )
{
i =5;
while(seats[i] > 0 && i < 9)
i++;
if(seats[i] == 9)
std::cout << " Sorry, All economy class seats are full.\n";
else
{
seats[ i ] = 1;
std::cout << "You are seat number:\n" ;
std::cout << "Your seat is in the ECONOMY SECTION, have a nice flight.\n";
}
}
}
return 0;
}

shaticus
12-14-2004, 08:34 AM
One last thing, i would break up this program into a few different functions.
One function for reserving first class seat & one for second class seats.
Then main would be responsible for reading the choice, and you could go in to a switch statement or just add function calls and call your program good.

Adding the functions should make debugging any problems you have a little easier as you will be able to tell what function the problem is in.