Getting a file into a double array?

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 is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//This program lets the user reserve airline tickets on a plane
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

int main()
{
	//variables
	ifstream inputFile;
	string filename;
	const int NUM_ROWS = 15;
	const int NUM_COLUMNS = 6;
	const int 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
		else if (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
	else if (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.
Last edited on
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 (unsigned int row = 0; row < NUM_ROWS; row++)
{
    for (unsigned int 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.
If you prefer a 1-dimensional array you may code:

1
2
3
4
for (unsigned int i = 0; i < NUM_ROWS * NUM_COLS; i++)
{
    cin >> firstNumbers[i];   // ... or wherever you want them to be entered.
}


Ah, and pay attention: cin from my example may be any other input stream if open like your inputFile.

If you want to write an arrays content into a file use operator<<() for output files instead of operator>>() for input files. F.e.:

cout << firstNumbers[i] << " "; // Write number on standard output followed by a space character.


EDIT: Added code-tags.
Last edited on
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.
Ahh I get it now. That makes much more sense. Thank you so much for your help.

Perhaps I misworded I mean to use the file outputs as what the seat prices are. Not input anything into the file as it already has what I need in it.
Topic archived. No new replies allowed.