need help with looping through array

Hey, I am having trouble with a part of a program. I am trying to read data from a file for airplane seats and then each set I read I want to run it through each element in an array called seats[13][6] to assign an x to the appropriate seat. For example a seat listed in the text file is R3C so it would go through each element in the seats array until it finds the matching seat and then marks its char value in seats to 'x' for that element. Here is my code --
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;



int main(){
//variables
int i = 0;

//array's
char seats[13][6];
char a[38];
int b[38];
char c[38];
int rowNum = 1;
//filling seats array


for (int row = 0; row < 13; row++)
{
for (int col = 0; col < 6; col++)
{

seats[row][col] = '*';

}
}

//reading the file
ifstream theFile("reservation.txt");

while (!theFile.eof())
{
//assigning values to each array
theFile >> a[i] >> b[i] >> c[i];
//loop to go through each element and if it is true it changes the element in seats array
for (int row = 0; row < 13; row++)
{
for (int col = 0; col < 6; col++)
{
if (a[i] == 'R' && b[i] == row + 1 && c[i] == 'E')
{

cout << a[i] << b[i] << c[i] << endl;
seats[row][col] = 'x';
}
}
}
i++;

}
theFile.close();
//Just to make sure the data from the files went into the three array's correctly
//for (int z = 0; z < 37; z++)
//{
// cout << a[z] << b[z] << c[z] << endl;
// }


for (int row = 0; row < 13; row++)
{
for (int col = 0; col < 6; col++)
{

cout << right << setw(2) << seats[row][col];
}
cout << endl;
}
std::cin.get();
return 0;
}


data for text file "reservation.txt."--

reservation.txt
R1C R1E R1F
R2B R2D R2F
R3C R3D R3F
R4A R4C R4E R4F
R5B R5D
R6B R6F
R7A R7E R7F
R8B R8D R8E
R9A R9C R9D R9F
R10B R10D R10E R10F
R11C R11E
R12C R12D R12F
R13E


Now for some reason when it finds a match for char 'R', row number and char 'E' as I used in this program to test it, it replaces the whole row instead of just the single element. Does anyone have any idea's or advice? I feel like it should be working but clearly I am doing something wrong. I also attached the reservation text file, thanks!

Your loop at lines 35-42 is flawed. Once you find a match, you continue testing with the same c[i], therefore subsequent compares will match because you have not advanced i.

Suggestion: Break this into two loops. One to read in a,b,c and then one to assign the seat map.

Line 31: Your test for eof() is also flawed. After you've read R13E from the file, the eof bit is NOT set even though you read the last logical record. Therefore your loop executes one more time. The input operation at line 33 fails, but you don't check that and you proceed as if it had worked thereby proceeding with invalid values in a[37],b[37] and c[37]. Better to do this as:
 
    while (theFile >> a[i] >> b[i] >> c[i])


Your program also assumes there will never be more than 38 seats reserved, however, there are 78 possible seats.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Yea after a bit I noticed that I had c[i] when I should have had c[col], not sure why I didn't notice it sooner. Thank you for your replies and I will remember the <> formatting button in the future!
Last edited on
Topic archived. No new replies allowed.