error C2440: ✀=✀ : cannot convert from ✀char *✀ to ✀int✀

2024-12-27 11:07:14
推荐回答(2个)
回答1:

#include //.h去掉
#include //添加这句
#include
using namespace std; //添加这句
class chemistry
{
char name[20];
float price;
int num;
public:
chemistry(char *na,float p,int n) //分号改为逗号
{
//name=na; 这句错了,改为如下 字符串不能直接赋值,要用函数strcpy
strcpy(name,na);
price=p;
num=n;
}//构造
char *getname()
{
return name;
}
float getprice()
{
return price;
}
int getnum()
{
return num;
}
void setname(char *np)
{
//name=np; //提示这里出错 改为如下 字符串不能直接赋值,要用函数strcpy
strcpy(name,np);
}
}; //添加这个表示类结束
int menu_select()//菜单
{
char *m[11]={"1.增加化学信息",
"2.修改化学信息",
"3.删除化学信息",
"4.查询化学信息",
"5.按化学品名称排序",
"6.显示全部化学品信息",
"7.库存预警查询",
"8.读取磁盘文件数据",
"9.写入磁盘文件数据",
"0.退出"};
int i;
int choice;
do{
system("cls");
for(i=0;m[i];i++)
cout<cout<<"请输入数字0~9。";
cin>>choice;
}while(choice<0||choice>9);
return(choice);
}

void main()
{
for(;;)
{
switch(menu_select())
{
case 1:
system("cls");
cout<<"1111"<system("pause");
break;
case 2:
system("cls");
cout<<"211"<system("pause");
break;
case 3:
system("cls");
cout<<"333"<system("pause");
break;
case 4:
system("cls");
cout<<"433"<system("pause");
break;
case 5:
system("cls");
cout<<"555"<system("pause");
break;
case 6:
system("cls");
cout<<"666"<system("pause");
break;
case 7:
system("cls");
cout<<"777"<system("pause");
break;
case 8:
system("cls");
cout<<"888"<system("pause");
break;
case 9:
system("cls");
cout<<"999"<system("pause");
break;
case 0:
system("cls");
cout<<"再见"<system("pause");
exit(0);
}
}
}

回答2:

这样改
#include
#include
int main()
{
char* new1(unsigned n,unsigned size);
void free1(void *p);
int n;
char *p;
printf("请输入n的值,给n个字符开辟连续的存储空间:");
scanf("%d",&n);
p=new1(n,1);
free1(p);
return 0;
}
char * new1(unsigned n,unsigned size)
{
char *p;
p=(char *)calloc(n,1);
return p;
}
void free1(void *p)
{
free(p);
}