C++. For example the user enters the name Fionna Issac as one whole string. This string is then to be entered into the two dimensional array seats[2][4].
C++. For example the user enters the name Fionna Issac as one whole string. This string is then to be entered into the two dimensional array seats[2][4].
Two dimensional array of what? Characters? Strings?
How is the string supposed to be split into that two dimensional array? According to the space between the first and last name?
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
I think the two dimensional array is irrelevant. Given the variable name "seats" it implies he has 2 rows by 4 columns of seats for the people read from the file.
There's a getline example on the first Google result (http://www.cplusplus.com/doc/tutorial/files/):
All you have to do is modify it so under getline you go seats[x][y] = line; x and y you can keep track of by incrementing them after seat assignment. eg.Code:// reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; }
Too much information? Save yourself a ++post_count.Code:const int MAX_ROWS = 2, MAX_COLUMNS = 4; int x = 0, y = 0; if(y < MAX_COLUMNS) { ++y; } else { if(x < MAX_ROWS) { ++x; y = 0; } else { std::cout << "No more seats available!" << std::endl; } }
Last edited by Dae; 12-23-2009 at 06:35 AM.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
Oh dear, that is actually a bad example. By using eof() to control the loop in that way, control enters the loop body one more time than it should. The reason is that after the last line is read, eof() returns false. It is only when control enters the loop and yet another getline() is performed does eof() return true, but by then the cout << line << endl; statement has been executed when it should not. Rather, the loop should be:Originally Posted by Dae
Code:while (getline(myfile, line)) { cout << line << endl; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)