我如何计算一连串整数使用数字多少次?

问题描述 投票:0回答:1

这似乎很容易,但是由于某种原因,我的代码无法正常工作。我尝试移动各个部分以查看发生了什么变化,修复了错误使用的变量,但是每次尝试构建和运行程序时,环境仍然崩溃。

这是我的代码:

int n, a, dgt, I, II, III, IV, V, VI, VII, VIII, IX;
    cout << "Enter an integer: \n";
    cin >> n;
    a = n;
    while (a > 0)                    // I use this cycle to seperate every number of the chain
    {
        while (n > 0)                // I use this cycle to analyze every number of the chain
        {
            dgt = n % 10;
            n = n / 10;
            if (dgt == 1) I ++;
            if (dgt == 2) II++;
            if (dgt == 3) III ++;
            if (dgt == 4) IV ++;
            if (dgt == 5) V ++;
            if (dgt == 6) VI ++;
            if (dgt == 7) VII ++;
            if (dgt == 8) VIII ++;
            if (dgt == 9) IX ++;
        }

        a--;
    }

我会很感激您能给我的任何建议:)

c++ int digits
1个回答
0
投票

好吧,问题出在代码中,而不是我如何运行程序。现在,它只计算给定数字的位数,而忽略整个链。我该怎么办?

© www.soinside.com 2019 - 2024. All rights reserved.