The assignment is to create a program that lets users reserve a spot on a plane either in coach or first class. I'm supposed to create a .txt file to use as the prices for different seats and while I have done that, I'm having trouble getting the file into a double array.
I hope I'm not breaking any rules or anything by posting this.
//This program lets the user reserve airline tickets on a plane
#include <iostream>
#include <cstring>
#include <fstream>
usingnamespace std;
int main()
{
//variables
ifstream inputFile;
string filename;
constint NUM_ROWS = 15;
constint NUM_COLUMNS = 6;
constint ARRAY_SIZE = 80;
string numbers[NUM_ROWS][NUM_COLUMNS];
int firstNumbers[ARRAY_SIZE];
int count = 0;
int reserve;
int economicChoice;
int row;
int column;
//opens file
inputFile.open("C:\\SeatPrices.txt");
//Reads the numbers from the file into the array.
while (count < ARRAY_SIZE && inputFile >> firstNumbers[count])
count++;
//Populating the double array
for (firstNumbers[count] = 0; firstNumbers[count] < ARRAY_SIZE; firstNumbers[count]++)
{
}
//Finds out whether the user wants to reserve a seat or not
cout << "Welcome to the Airline Reservation System.\n";
cout << "What would you like to do?\n";
cout << "Reserve a seat (1)\n";
cout << "See total Sales (2)\n";
cout << "Exit (3)\n";
cin >> reserve;
//validates the question of reserving a seat or not
while (reserve < 1 || reserve > 3)
{
cout << "Error: Invalid Number.\n";
cout << "Enter in '1' to reserve a seat";
cout << "2 to see total sales.\n";
cout << "Or 3 to exit.\n";
cin >> reserve;
}
if (reserve == 1)
{
cout << "Would you like to reserve a spot in First Class (3) or Coach (4)?";
cin >> economicChoice;
//first class choices
if (economicChoice == 3)
{
cout << "Great!\n";
cout << "Here are youre options: (# = open seat, * = seat already taken)" << endl;
cout << " 1 2 3 4\n";
cout << "1. * * * *\n";
cout << "2. * * # #\n";
cout << "3. * # # *\n";
cout << "4. * # # #\n";
cout << "5. # # * *\n";
cout << "What row would you like to sit in? (1-5)\n";
cin >> row;
cout << "What seat would you like to sit in? (1-4)\n";
cin >> column;
// Validates options
while (column < 1 || column > 4)
{
cout << "Error: Invalid Number Input.\n";
cout << "Please enter a proper column number!\n";
cin >> column;
}
while (row < 1 || row > 5)
{
cout << "Error: Invalid Number Input.\n";
cout << "Please enter a proper row number!\n";
cin >> row;
}
}
//economy class choices
elseif (economicChoice == 4)
{
cout << "Good Choice!\n";
cout << "Here are your options: (# = open seat, * = seat already taken)\n";
cout << " 1 2 3 4 5 6\n";
cout << " 6. * * # # # *\n";
cout << " 7. # # # # # #\n";
cout << " 8. # # * # * *\n";
cout << " 9. * * * # * *\n";
cout << "10. # # # # # #\n";
cout << "11. * * # # # *\n";
cout << "12. * * # # # #\n";
cout << "13. # # # # * *\n";
cout << "14. * # # # * *\n";
cout << "15. # # # * * *\n";
cout << "What row would you like to sit in? (6-15)\n";
cin >> row;
cout << "What seat would you like to sit in? (1-6)\n";
cin >> column;
//Validates options
while (column < 1 || column > 6)
{
cout << "Error: Invalid Number Input.\n";
cout << "Please enter a proper column number!\n";
cin >> column;
}
while (row < 6 || row > 15)
{
cout << "Error: Invalid Number Input.\n";
cout << "Please enter a proper row number!\n";
cin >> row;
}
}
}
//option 2 of reserving a seat
elseif (reserve == 2);
{
cout << "Maybe next time!" << endl;
}
}
Once I have the file into a double array, I have to use that array to implement it into the seat prices. Such as if user chooses to sit in row 1 seat 1 it would be that price. Also the '#' means open seat and '*' means seat taken. Thanks!
O yea, under the "//Populating the double array" is me trying to see if a for statement or a nested for statement would work.
Maybe I misunderstand your problem. If you want to read the contents of a file of 15 rows with 6 numbers each into the array, then you may try nested for-loops:
1 2 3 4 5 6 7
for (unsignedint row = 0; row < NUM_ROWS; row++)
{
for (unsignedint col = 0; col < NUM_COLS; col++)
{
cin >> numbers[row][col];
}
}
If the array should really contain numbers you may want to declare its items of type int or double instead of string.
Will that hold the numbers from the file into the array?? In the code I posted I was thinking I could post it into a single array then switch it into a two-dimmensional array. And also yea I was hoping to implement the numbers from the file in the array into the seating later on so that somehow I could turn it to where the user can see the "cost" of the seat.
Have a look at Cplusplus.com Reference manual for input/output for a detailed usage of C++ IO-libraries. Especially error and end-of-file handling are important. There are lots of examples given.