字符异味常处理

来源:互联网 发布:雨人医药软件好不好 编辑:程序博客网 时间:2024/05/05 09:08

c++中char类型变量如何转换成int类型变量

例子:
char str_tmp;
int int_tmp;
str_tmp='5';
int_tmp=(int)str_tmp;
cout<<int_tmp<<endl;
输出的是5的ASCII码值53,怎么才能让int_tmp==5而不是53呢?
char str_tmp;
int int_tmp;
str_tmp='5';
int_tmp=(int)(str_tmp - '0');
cout<<int_tmp<<endl;

getchar()问题
#include "stdio.h"
#include "conio.h"
main()
{
char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters/n");
while((c=getchar())!='/n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d/n",letters,
space,digit,others);
getch();
}

最后一个getch();是为了让程序在执行后那个printf()以后,停止在那里,不会立刻关掉窗口。 
原创粉丝点击