c++ - Array List printing wrong numbers -
i new c++ , trying write program take in set of results of exam , print them out in histogram. writing code in stages , @ moment trying take in exam marks , print them out in list make sure working before moving on histogram.
my problem when input numbers array , print them weird number coming up, example enter numbers 1,2,3,4
expected console output: 1 2 3 4
actual output: -858993460 1 2 3 4
so know must problem code not sure can please?
code:
void readexammarks(int exammarks[], int sizeofarray){ cout << "please enter set of exam marks see histogram for:" << endl; for( int x = 0; x < sizeofarray; x++){ cin >> x; exammarks[x] = x; } } void printexammarks(int exammarks[], int sizeofarray){ system("cls"); for(int x = 0; x < sizeofarray; x++){ cout << exammarks[x] << endl; } } int main() { int exammarks[5]; readexammarks(exammarks, 5); printexammarks(exammarks,5); system("pause"); }
you reusing x
both array index , data:
for( int x = 0; x < sizeofarray; x++){ cin >> x; exammarks[x] = x; }
you need use separate variable array index:
int x = 0; for( int idx = 0; idx < sizeofarray; idx++){ cin >> x; exammarks[idx] = x; }
Comments
Post a Comment