Where is the mistake? Why numbers are not written to the array and not displayed later?

#include <iostream> using namespace std; int main() { int k = 0, ch, a; int array[123]; cin >> ch; while (ch > 0) { a = ch % 10; ch = ch / 10; array[k++] = a; } cout << array[k]; system("PAUSE"); return 0; } 

    3 answers 3

    0 output? Well, probably because you need to make the right conclusion

     for(int j = 0; j < k; j++) { cout << array[j]; } 

    not just cout << array[k];

    • thank! And how can you make it so that it also displays the number of elements removed? - navi1893
    • one
      enter that one item. Maybe numbers? then it will be k cout << k << endl; - KoVadim
    • Why did you decide that they are not recorded?
    • You output one element of the array, and after the last one filled, there is garbage.
    • Not enough output of the end of line character.
       #include <iostream> using namespace std; int main() { int k = 0, ch, a; int array[123]; cin >> ch; while ( ch > 0 ) { a = ch % 10; ch = ch / 10; array[k++] = a; } for (int i=k-1; i>=0; i--) { cout << array[i] ; } system( "PAUSE"); return 0; }