C++ C++;函数重载无法识别字符

C++ C++;函数重载无法识别字符,c++,C++,当我输入两个整数时,输出正确地表示它们的差。然而,当我输入一个字符串和一个字符时,它返回的不是字符在字符串中出现的次数,而是-1,这是错误的输出。谁能帮帮我吗?这是我第二天学习C++……/P> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> void mycount(int a, int b) { std::cout<&l

当我输入两个整数时,输出正确地表示它们的差。然而,当我输入一个字符串和一个字符时,它返回的不是字符在字符串中出现的次数,而是-1,这是错误的输出。谁能帮帮我吗?这是我第二天学习C++……/P>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

void mycount(int a, int b)
{
        std::cout<< a - b <<std::endl;
}

void mycount(char str[], char s[])
{
        int len,i;
        int sum=0;

        len = strlen(str);
        for (i=0;i<len;i++){
            if (strncmp(&str[i],&s[0],1) == 0){
            sum = sum + 1;

};
};
printf("results: %d times\n",sum);
}

int main()
{
        int a,b;
        char c[200],d;
        if(std::cin>> a >> b){
            mycount(a,b);
        }
        if(std::cin>> c[200] >> d){
            mycount(a,b);
        }
        else{
            std::cout<< "-1" <<std::endl;
        }
        std::cin.clear();
        std::cin.sync();
}
#包括
#包括
#包括
#包括
无效mycount(整数a、整数b)
{
标准::cout b){
我的账户(a、b);
}
如果(标准::cin>>c[200]>>d){
我的账户(a、b);
}
否则{

std::cout提示-此程序将打印什么

#include <iostream>

using namespace std;

int main()
{

    char c[200],d;

    cout << sizeof(c) << endl;
    cout << sizeof(d) << endl;

   return 0;
}
c是指向int的指针,d是int


既然你正在做C++,为什么不让你的生活更简单,使用STD::string:

< p>一些问题应该解决你的问题。首先,输入一个数组,使用<代码> CIN < /代码>使用<代码> GETLION>代码>,然后在前面调用“<代码>忽略< /代码>”。你第二次用c和d代替a和b计数

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

void mycount(int a, int b)
{
        std::cout<< a - b <<std::endl;
}

void mycount(char str[], char s)
{
        int len,i;
        int sum=0;

        len = strlen(str);
        for (i=0;i<len;i++){
            if (strncmp(&str[i],&s,1) == 0){
            sum = sum + 1;

};
};
printf("results: %d times\n",sum);
}

int main()
{
        int a,b;
        char c[200],d;
        if(std::cin>> a >> b){
            mycount(a,b);
        }
         std::cin.ignore();
        if(std::cin.getline (c,200) && std::cin >> d){
            mycount(c,d);
        }
        else{
            std::cout<< "-1" <<std::endl;
        }
        std::cin.clear();
        std::cin.sync();
}
#包括
#包括
#包括
#包括
无效mycount(整数a、整数b)
{
标准::cout b){
我的账户(a、b);
}
std::cin.ignore();
如果(标准::cin.getline(c,200)和标准::cin>>d){
mycount(c,d);
}
否则{

std::coutWhy试图从
cin
提取到
c[200]
?这超出了范围。我最好的猜测是,您输入的是整个字符串,而
c[200]
只是一个
char
。将设置
eof
标志,并且
cin
将计算为
false
@JorenHeit。编写一个程序,提供两个名为mycount()的重载函数。如果传递了整数,则此函数计算第一个参数和第二个参数之间的差值;如果传递了字符串和字符,则此函数计算字符的出现次数。您永远不会传递字符串
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

void mycount(int a, int b)
{
        std::cout<< a - b <<std::endl;
}

void mycount(char str[], char s)
{
        int len,i;
        int sum=0;

        len = strlen(str);
        for (i=0;i<len;i++){
            if (strncmp(&str[i],&s,1) == 0){
            sum = sum + 1;

};
};
printf("results: %d times\n",sum);
}

int main()
{
        int a,b;
        char c[200],d;
        if(std::cin>> a >> b){
            mycount(a,b);
        }
         std::cin.ignore();
        if(std::cin.getline (c,200) && std::cin >> d){
            mycount(c,d);
        }
        else{
            std::cout<< "-1" <<std::endl;
        }
        std::cin.clear();
        std::cin.sync();
}