一个C++的问题

来源:百度知道 编辑:UC知道 时间:2024/05/19 01:23:36
今天第一次用返回值是Char的,遇到一个问题。
题目是,输入一个数,判断是否是素数。
我的代码是:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
char d;
int main()
{ int x;
char abc(int x);
char a;
cin>>x;
a=abc(x);
cout<<a<<endl;
return 0;
}
char abc(int x)
{ bool t=true;
int a;
if(x>=1)
{ for(a=2;a<=sqrt(x);a++)
{if(x%a==0)
t=false;break;
}
if(t)
d="sushu"; //这里
else
d="bushisushu"; //这里
}
else
{d="wrong";} //这里
return d;
}

编译时出错:提示invalid conversion from 'const char*'to 'char'

谁告诉我应该怎么改,谢谢,我想把返回值先传给主函数然后显示出来,不想直接显示结果。
怎么改?

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
char msg[] = "sushu";
char msg1[] = "bushisushu";
char msg2[] = "wrong";
char *d;

char* abc(int x);
int main()
{
int x;

char* a;
cin>>x;
a=abc(x);
cout<<a<<endl;
return 0;
}
char* abc(int x)
{
bool t=true;
int a;
if(x>=1)
{
for(a=2;a<=sqrt(x);a++)
{
if(x%a==0)
t=false;break;
}
if(t)
d=msg; //这里
else
d=msg1; //这里
}
else
{
d=msg2;
} //这里
return d;

}

/* sushu bushisushu wrong 都是字符串常量,不能赋值给char类型
sushu bushisushu wrong 分别申明为全局变量,
char* abc(int x) 中返回它们的首地址指针就可以了。
*/

char abc()这个函数返回值是char 字符型
但是你return的d却是字符串(字符指针)
所以