2个简单的C++问题。。。

来源:百度知道 编辑:UC知道 时间:2024/09/27 19:25:15
1 写一个函数,用户可能输入,如果数字把他们相加,如果是字符则提示错误,并且返回
2 char *msg="hello world!";
size of(msg)=?

1,
#include<stdio.h>
main()
{
char a,b,sum;
scanf("%c %c",&a,&b);
if(a>='0' && a<='9' && b>='0' && b<='9')
{sum=a+b;
printf("%d",sum-96);}
else
printf("ERROR");
}

2,
4

1.
#include<iostream>
using namespace std;

int Fun()
{
char c1,c2;
int sum = 0;

cin >> c1;
cin >> c2;

if(isdigit(c1) && isdigit(c2))
{
sum = c1 - '0'+ c2 - '0';
}
else
{
return -1;
}

return sum;

}
int main()
{
int ReturnVal = 0;

ReturnVal= Fun();
if(ReturnVal != -1)
{
cout <<"和:"<<ReturnVal<<endl;
}
else
{
cout <<"字符错误!"<<endl;
}

return 0;