dimanche 25 février 2018

Using an Array[10] and incrementing it with a While loop using modulo 10 to Pair User inputs together

#include <iostream>
using namespace std;

int main()
{
    long int number;
    int digits;
    cout << "Enter Number: ";
    cin >> number;
    int counter[10] = { 0,0,0,0,0,0,0,0,0,0 };
    while (number != 0) {
        digits = number % 10;
        counter[digits] = counter[digits] + 1;
        number = number / 10;
    }
    for (int i = 0; i<10; i++) {
        if (counter[i] != 0) {
            cout << i << ": " << counter[i] << endl;
        }
    }
    return 0;
    system("pause");
}

I'm having an issue with my code that when I run it and enter a Number nothing really happens. It is supposed to run something like 1234556789 and the output should look like 1 : 9 2 : 8 3 : 7 4 : 6 5 : 5

I know sometimes if there isn't a system pause this happens where it runs part of the code and just ends, but I'm not sure whats wrong here.

Aucun commentaire:

Enregistrer un commentaire