c++ errorC2015: too many characters in constant

来源:百度知道 编辑:UC知道 时间:2024/05/27 00:18:09
统计字符变量的
#include<iostream.h>
#include<stdio.h>
void main()
{
char ch[100];
gets(ch);
int i=0,j=0,k=0,l=0;int m=1;
for(;ch[m]!='\n';m++)
{

if('A'<='ch[m]'<='Z'&&'a'<='ch[m]'<='z')
i++;
if('0'<='ch[m]'<='9')
j++;
if('ch[m]'==' ')
k++;
else l++;

}
cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<endl;
}

F:\c++\3-6\3-6.cpp(11) : error C2015: too many characters in constant
F:\c++\3-6\3-6.cpp(11) : error C2015: too many characters in constant
F:\c++\3-6\3-6.cpp(13) : error C2015: too many characters in constant
F:\c++\3-6\3-6.cpp(15) : error C2015: too many characters in constant <

用''括起来的只能有一个字符,如'c', 'c['就要出错,当你要使用某个字符变量时,直接用变量名就行了,不用使用单引号
如有 char ch='b';
在使用时这样就行了 if ('a'< ch)
你这三行这样改就行了
11: if('A'<=ch[m] && ch[m]<='Z'&&'a'<=ch[m] && ch[m]<='z')
13: if('0'<=ch[m] && ch[m]<='9')
15: if(ch[m]==' ')
顺便说一句,在C语言类似于 a<b<c 这种表达式不能直接这么写
要写成以下形式:
a<b && b<c

#include<iostream.h>
#include<stdio.h>
#include<string.h>
void main()
{

char ch[100];

gets(ch);

int i=0,j=0,k=0,l=0;
int m=1;
int len = strlen(ch);
for(;ch[m] != '\0'; m++)
{
if( (ch[m] >= 'A' && ch[m] <='Z') || (ch[m] >= 'a' && ch[m]<='z'))
i++;
else if( ch[m] >= '0' && ch[m] <= '9')
j++;
else if(ch[m]== 32)
k++;