有关c++的一个问题不知哪里出错了求教?

本人学生课本有一道题

请编写程序,实现统计字符串s中数字字符‘0’~‘9’出现的次数,并依次存储在长度为10的数组count中。

以下是本人编写的程序,请指教。

 

#include <stdio.h>
void main()
{
    char c;
    int count[10] = {0,0,0,0,0,0,0,0,0,0};
    printf("请输入任意字符(输入*终止)\n");
    while ((c = getchar()) != '*');
    {
        if (c == 0)
            count[0]++;
        else if (c == 1)
            count[1]++;
        else if (c == 2)
            count[2]++;
        else if (c == 3)
            count[3]++;
        else if (c == 4)
            count[4]++;
        else if (c == 5)
            count[5]++;
        else if (c == 6)
            count[6]++;
        else if (c == 7)
            count[7]++;
        else if (c == 8)
            count[8]++;
        else if (c == 9)
            count[9]++;
        else;
    }
    printf("%d %d %d %d %d %d %d %d %d %d", count[0], count[1], count[2], count[3], count[4], count[5], count[6], count[7], count[8], count[9]);
}
 

正常应该是输入55566677789输出0000033311,可是每次不够输入什么输出都为0000000000

 

#include <stdio.h>
void main()
{
    char c;
    int count[10] = {0,0,0,0,0,0,0,0,0,0};
    printf("请输入任意字符(输入*终止)\n");
    while ((c = getchar()) != '*')
    {
        if (c == '0')
            count[0]++;
        else if (c == '1')
            count[1]++;
        else if (c == '2')
            count[2]++;
        else if (c == '3')
            count[3]++;
        else if (c == '4')
            count[4]++;
        else if (c == '5')
            count[5]++;
        else if (c == '6')
            count[6]++;
        else if (c == '7')
            count[7]++;
        else if (c == '8')
            count[8]++;
        else if (c == '9')
            count[9]++;
        else;
    }
    printf("%d %d %d %d %d %d %d %d %d %d", count[0], count[1], count[2], count[3], count[4], count[5], count[6], count[7], count[8], count[9]);
}

 

既然用C++,就要真的用C++,而不是C。你看C++代码简单多了。


#include <map>
#include <iostream>

using namespace std;

int main()
{
    map<char, int> counter;
    char c;
    
    cout << "请输入任意字符(输入*终止: " << endl;
    while ((cin >> c) && c != '*')
    {
        ++counter[c];
    }
    
    cout << "Digit counters: ";
    for (c = '0'; c <= '9'; ++c)
        cout << counter[c] << ' ';
    cout << endl;
    
    return 0;
}

// Output
请输入任意字符(输入*终止:                                                                                                                                                         
55566677789*                                                                                                                                                                       
Digit counters: 0 0 0 0 0 3 3 3 1 1    

 

如果你满意我的回答,请点采纳,我很感谢你的认可。

代码的问题一是while ()后面多了个 ; 分号,二是if (c == '0') 其中'0'是字符要加单引号。

getchar 返回的是一个字符对应的 ASCII 的编码,而字符 '1' 对应的编码应该是 49 。你要把所有的 == 号的右边都改成单引号扩起来的

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

希望对您有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html