Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

C++. I am writing a multidimensional array to store data; name, surname, account number of users. The array below does not print out the correct results.

OpenStudy (anonymous):

plz help. #include <iostream> using namespace std; int main () { char array1 [2][5]; char name[20]; char surname [20]; for (int i = 0; i < 2; i++ ) { for ( int j = 0; j < 5; j++) { //reads entered name cin.getline (name,20); // assign entered name into the multi dimensional array array1 [i][j] = name [j]; } } cout << endl; cout << " contents of array " << endl; // prints out the results of the multi dimensional array for (int k = 0; k < 2; k++ ) { for ( int m = 0; m < 5; m++) { cout<< array1 [k][m] << " " ; } cout<<endl; } }

OpenStudy (e.mccormick):

Well, how large of a name are you putting in, because your buffers are very different sizes from the multi-dimensional array. Also, what are you doing to handle the nul character or new line. Those are two things I see off the top of my head.

OpenStudy (anonymous):

i actually want to put data like " John Smith 0362546213". but i was testing with a name of 20 characters

OpenStudy (anonymous):

You are not calling `getline` in the right place. It shouldn't be in the loop. Also, your array shouldn't be only 5 characters if you expect 20 character input.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!