I am have trouble getting this code to work in VS 2010 can someone help me and show me what I am doing wrong?
#include <iostream> #include <fstream> using namespace std; const int maxRows = 5, maxCols = 6; void readNames(string seats[ ][maxCols]); void display(string seats[ ][maxCols]); void switchStudents(string seats[ ][maxCols]); int main( ) { int choice; string seats[maxRows][maxCols]; readNames(seats); bool finished = false; cout << "Seating Chart Program " << endl; while (!finished) { cout << "1 - Display the Seating Chart" << endl; cout << "2 - Switch 2 students" << endl; cout << "3 - Quit" << endl << endl; cout << "Enter your choice (1-3): "; cin >> choice; switch (choice) { case 1: display(seats); break; case 2: switchStudents(seats); break; case 3: finished = true; break; default: cout << "incorrect response. Please reenter"; } } system("pause"); return 0; } void readNames(string seats[][6]) { ifstream filein("C:\\seats.dat"); for (int row = 0; row< maxRows; row++) for (int col = 0; col< maxCols; col++) filein >> seats[row][col]; filein.close( ); } void display(string seats[ ][6]) { cout << "\n 0 1 2 3 4 5\n"; for ( int r = 0; r < maxRows; r++) { cout << r << " "; for (int col = 0; col < maxCols; col++) { cout.width(12); cout.setf(ios::left); cout << seats[r][col]; } cout << endl; } cout << endl << endl; } void switchStudents(string seats[][6]) { int r1, c1, r2, c2; cout << "\nEnter row & col for first student: "; cin>> r1 >> c1; cout << "\nEnter row & col for second student: "; cin >> r2 >> c2; string temp = seats[r1][c1]; seats[r1][c1] = seats[r2][c2]; seats[r2][c2] = temp; }
Please use a code paste service like dpaste, pastebin, or gihub's gist. See, if I copy and paste this code to test it, the result all comes out on one line. But if you use a code paste service, it comes out fine.
Can you help me with this please....
filein >> seats[row][col]; It does not like that use of >>
Probably because you don't pass an array to a function... you pass a pointer. This could make a type mismatch. Rather than doing it as a stream, you could take in each line, parse it, and assign things.
so what would you recommend?
This talks about use of arrays in functions: http://www.cplusplus.com/forum/articles/20881/
Can u help me?
What is he talking about?
Well, what is the seats.dat you are taking in?
void display(string seats[ ][6]) I don't know anything about this language, but there you appear to have a space whereas you don't where you did similar functions.. hope it helps?
Right before the 6 there, "seats[ ][6])" Here you have the same setup but not the same syntax "void readNames(string seats[][6])"
Join our real-time social learning platform and learn together with your friends!