本应打印大小写数字,但返回1并退出程序

Supposed to print number of upper and lowercase but returns 1 and exits program

本文关键字:返回 退出程序 打印 大小写 数字      更新时间:2023-10-16

在用void函数编写程序之前,程序一直在运行。我不知道我哪里搞砸了。它返回1并且没有给出其他错误。

#include <iostream>
#include <string>
using namespace std;
char response;
string s;
int upper, lower, other, count;
void capCheck(string);
int main()
{
    count = 0;
    upper = 0;
    lower = 0;
do
{
    cout<<"Get the number of upper and lower case letters in your sentence!!"<<endl;
    cout<<endl;
    cout<<"Type your sentence below without spaces.."<<endl;
    cin>>s; 
    capCheck(s);    
    cout<<"Would you like to continue? Y/N"<<endl;
    cin>>response;
}while(response == 'y' || response == 'Y');
return 0;    
}
void capCheck()
{
    while(s[count] != 0)
    {
        if(s[count] >= 'a' && s[count] <= 'z')
        {
            lower++;
            count++;
        }
        else if (s[count] >= 'A' && s[count] <= 'Z')
        {
            upper++;
            count++;
        }
        else
            other++;
    }
    cout<<"The number of uppercase letters are: "<<upper<<endl;
    cout<<"The number of lowercase letters are: "<<lower<<endl; 
}

只需在函数声明中将void capCheck()更改为void capCheck(string s)即可。它对我来说很好。

对代码的一些评论:尽量不要使用全局变量,并提高独立性。

在您的函数定义中放入

void capCheck(string s) { 
    // ... 
}

观看现场演示。

提供的功能定义签名

void capCheck() { 
    // ... 
}

与函数原型的签名的实际声明不匹配

void capCheck(string);

"我不知道我在哪里搞砸了。它返回1,没有其他错误。"

该程序没有按照您发布代码的形式进行编译。构建过程可能提前停止(请注意,main()函数中没有任何返回1的路径)。