2014-09-30 82 views
0

我有程序工作,直到我用void函数编写它。我不知道我在哪里搞砸了。它返回1,并没有给出其他错误。假设打印大写和小写数字,但返回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; 
} 
+0

那么代码甚至不为我编译。 – 2014-09-30 19:37:19

+1

你的声明和'capCheck'的定义不匹配,你应该学会在不使用全局变量的情况下做到这一点。 – crashmstr 2014-09-30 19:38:13

回答

1

在你的函数声明只要改变void capCheck()void capCheck(string s)。这对我来说可以。

对代码的一些评论:尽量不要使用全局变量并改进缩进。

+0

那有效的朋友,谢谢 – FrankyFigs 2014-09-30 19:40:49

+0

然后pelase标记是被接受的答案。 – 2014-09-30 19:44:39

+0

@JaviV那么,我的配偶不应该接受吗? :。(... – 2014-09-30 19:54:21

1

在你的函数定义放在

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

live demo

所提供的函数定义签名

void capCheck() { 
    // ... 
} 

不符合您的函数原型签名的实际申报

void capCheck(string); 

“我不知道在哪里,我搞砸它返回1,并没有给出其他错误。“

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