C++ array with wrong input -
so wrote program code read in number of ints, , supposed read array backwards. however, reads 0s
while (true) { cin >> input; if (input == -1 && gotstuff == false) {return 0;} else if (input == -1 && gotstuff == true) {break;} else { inputdata[pos1] = input; pos1 ++; gotstuff = true; } } (int = pos1; >= 0; i--) { outputdata[pos2] = inputdata[pos1]; pos2 ++; cout << outputdata[pos1] << " "; }
why reading 0s?
you have clear problem here
for (int = pos1; >= 0; i--) { outputdata[pos2] = inputdata[pos1]; pos2 ++; cout << outputdata[pos1] << " "; }
because try print outputdata[pos1]
pos1
never changed within loop. therefore output same character. copying same character of inputdata
outputdata
array when loop exits, outputdata
contain same character @ different indices.
you might have better luck following loop @ least copies , prints correct characters
pos2 = 0; (int = pos1-1; >= 0; i--) { outputdata[pos2] = inputdata[i]; cout << outputdata[pos2] << " "; pos2++; }
you may have other problems code not show in question values of pos1
, pos2
have been initialised with.
Comments
Post a Comment