-
looping help
I'm having a little trouble with this code. I've been assigned to make an Airline Reservations System script. Here's what I have to do:
There's a 10 element array, and that represents the seating chart on the plane. All elements are set to 0. When a person takes a seat, that element is substituted with a 1 to declare that the seat has been filled. There are two seating sections: First Class and Economy. A person can decide what section they want their seat in. If First Class has been completely filled, then the program asks if it should assign a seat to the Economy. If yes, then a seat in Economy gets filled. If no, a message is displayed.
Here's what I have so far:
Code:
#include <iostream.h>
#include <windows.h>
int main (int argc, char *argv[])
{
// Define the input values
string economy_input;
int input;
int counter = 0;
// Define the arrays
int firstclass[5] = {0, 0, 0, 0, 0};
int economy[5] = {0, 0, 0, 0, 0};
while (counter != -1)
{
for (int i = 0; i < sizeof(firstclass); i++)
{
cout << "Please type in 1 for \"First Class\" or 2 for \"Economy\"" << endl;
cin >> input;
if (input == 1) // "First Class" has been entered
{
if (firstclass[i] == 0 && firstclass[i] <= sizeof(firstclass)) // Checks to make sure that there is a spot left in the array
{
firstclass[i] = 1; // Fill in the element
cout << "Assigning the person's seat to: " << i << " in the first class section." << endl;
}
else
{
cout << "All of the first class spaces have been filled. Would you like to go to economy? Yes or No" << endl;
getline(cin,economy_input);
if (economy_input == "No")
{
cout << "Next flight leaves in 3 hours." << endl;
}
if (economy_input == "Yes")
{
if (economy[i] == 0 && economy[i] <= sizeof(economy))
{
economy[i] = 1;
cout << "Assigning the person's seat to: " << i << endl;
}
}
}
}
if (input == 2) // "Economy" has been entered
{
for (int i = 0; i < sizeof(economy); i++)
{
if (economy[i] == 0 && economy[i] <= sizeof(economy))
{
economy[i] = 1;
cout << "Assigning the person's seat to: " << i << " in the economy section." << endl;
}
}
}
}
return 0;
}
I'm not sure how the for loops and while loop would work for this script. I don't even know if there are suppose to be for loops/while loops. If someone could help me fix this up, it would be much appreciated.
-
Quote:
#include <iostream.h>
this header file is outdated - you should use more recent compiler
firstclass[i] <= sizeof(firstclass)
obviosly wrong
i < sizeof(firstclass)
wrong as well - on another reason
sizeof(firstclass) = 5* sizeof (int) which is 20 on most compilers
and yes - your for loops are messed up
just think what wil happen if the user selects 5 economy class sits first... He will not be able to buy firstclass at all
-
Also, what do you need <windows.h> for? I don't see any Windows API calls in there.
Since you're using getline() you should #include <string>
And since you don't explicitly qualify things like std::cout std::string or std::getline you need to add using namespace std; after your #include directives.
Lastly, you also have some parts of the code that aren't indented properly, which could make it harder to read.
-
Thanks for the suggestions. I've modified the script a bit.
Code:
#include <iostream>
#include <string>
using namespace std;
int main (int argc, char *argv[])
{
string economy_input;
int input;
int seats[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int size = sizeof(seats) / sizeof(int);
for (int i = 0; i < size; i++)
{
cout << "Please type in 1 for \"First Class\" or 2 for \"Economy\"" << endl;
cin >> input;
if (input == 1)
{
if (seats[i] == 0)
{
seats[i] = 1;
cout << "Assigning the person's seat to: " << i << " in the first class section." << endl;
}
else
{
cout << "All of the first class spaces have been filled. Would you like to go to economy? Yes or No" << endl;
getline(cin,economy_input);
if (economy_input == "No")
{
cout << "Next flight leaves in 3 hours." << endl;
}
}
}
if (input == 2)
{
seats[i] = 1;
cout << "Assigning the person's seat to: " << i << " in the economy section." << endl;
}
}
return 0;
}
I changed it to a single array since the problem is asking for a single-subscripted array.
My problem is that I don't know how to mark the elements to 1. First Class is seats 1-5 and Economy is 6-10. So if a person types in 2, then element 6 in the array should be set to 1. That's the part that I don't know how to do. The same would go with First Class.
-
I suppose you better go with 2 counters
Code:
int economyIndex = 5;
int firstClassIndex = 0;
and some while loop like
Code:
while(firstClassIndex <5 || economyIndex <10)
{
// there are some places left
//ask sit type
if(Firstclass)
{
if(firstClassIndex == 5)
{
//no sits left - suggest economy
}
else
{
//take place firstClassIndex and increase the index
}
}
else
{
//economy class
if(economyIndex <10)
{
//take place economyIndex and increase the index
}
else
{
//no economy class sits left - warn and continue
}
}
}