error C2440: '=' : cannot convert from 'char *' to 'int'
#include<iostream.h>#include<stdlib.h>classchemistry{charname[20];floatprice;intnum;p...
#include<iostream.h>
#include<stdlib.h>
class chemistry
{
char name[20];
float price;
int num;
public:
chemistry(char *na;float p;int n)
{
name=na;
price=p;
num=n;
}//构造
char *getname()
{
return name;
}
float getprice()
{
return price;
}
int getnum()
{
return num;
}
void setname(char *np)
{
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<<m[i]<<endl;
cout<<"请输入数字0~9。";
cin>>choice;
}while(choice<0||choice>9);
return(choice);
}
void main()
{
for(;;)
{
switch(menu_select())
{
case 1:
system("cls");
cout<<"1111"<<endl;
system("pause");
break;
case 2:
system("cls");
cout<<"211"<<endl;
system("pause");
break;
case 3:
system("cls");
cout<<"333"<<endl;
system("pause");
break;
case 4:
system("cls");
cout<<"433"<<endl;
system("pause");
break;
case 5:
system("cls");
cout<<"555"<<endl;
system("pause");
break;
case 6:
system("cls");
cout<<"666"<<endl;
system("pause");
break;
case 7:
system("cls");
cout<<"777"<<endl;
system("pause");
break;
case 8:
system("cls");
cout<<"888"<<endl;
system("pause");
break;
case 9:
system("cls");
cout<<"999"<<endl;
system("pause");
break;
case 0:
system("cls");
cout<<"再见"<<endl;
system("pause");
exit(0);
}
}
}
为啥错了? 展开
#include<stdlib.h>
class chemistry
{
char name[20];
float price;
int num;
public:
chemistry(char *na;float p;int n)
{
name=na;
price=p;
num=n;
}//构造
char *getname()
{
return name;
}
float getprice()
{
return price;
}
int getnum()
{
return num;
}
void setname(char *np)
{
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<<m[i]<<endl;
cout<<"请输入数字0~9。";
cin>>choice;
}while(choice<0||choice>9);
return(choice);
}
void main()
{
for(;;)
{
switch(menu_select())
{
case 1:
system("cls");
cout<<"1111"<<endl;
system("pause");
break;
case 2:
system("cls");
cout<<"211"<<endl;
system("pause");
break;
case 3:
system("cls");
cout<<"333"<<endl;
system("pause");
break;
case 4:
system("cls");
cout<<"433"<<endl;
system("pause");
break;
case 5:
system("cls");
cout<<"555"<<endl;
system("pause");
break;
case 6:
system("cls");
cout<<"666"<<endl;
system("pause");
break;
case 7:
system("cls");
cout<<"777"<<endl;
system("pause");
break;
case 8:
system("cls");
cout<<"888"<<endl;
system("pause");
break;
case 9:
system("cls");
cout<<"999"<<endl;
system("pause");
break;
case 0:
system("cls");
cout<<"再见"<<endl;
system("pause");
exit(0);
}
}
}
为啥错了? 展开
2个回答
展开全部
#include<iostream> //.h去掉
#include<string.h> //添加这句
#include<stdlib.h>
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<<m[i]<<endl;
cout<<"请输入数字0~9。";
cin>>choice;
}while(choice<0||choice>9);
return(choice);
}
void main()
{
for(;;)
{
switch(menu_select())
{
case 1:
system("cls");
cout<<"1111"<<endl;
system("pause");
break;
case 2:
system("cls");
cout<<"211"<<endl;
system("pause");
break;
case 3:
system("cls");
cout<<"333"<<endl;
system("pause");
break;
case 4:
system("cls");
cout<<"433"<<endl;
system("pause");
break;
case 5:
system("cls");
cout<<"555"<<endl;
system("pause");
break;
case 6:
system("cls");
cout<<"666"<<endl;
system("pause");
break;
case 7:
system("cls");
cout<<"777"<<endl;
system("pause");
break;
case 8:
system("cls");
cout<<"888"<<endl;
system("pause");
break;
case 9:
system("cls");
cout<<"999"<<endl;
system("pause");
break;
case 0:
system("cls");
cout<<"再见"<<endl;
system("pause");
exit(0);
}
}
}
#include<string.h> //添加这句
#include<stdlib.h>
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<<m[i]<<endl;
cout<<"请输入数字0~9。";
cin>>choice;
}while(choice<0||choice>9);
return(choice);
}
void main()
{
for(;;)
{
switch(menu_select())
{
case 1:
system("cls");
cout<<"1111"<<endl;
system("pause");
break;
case 2:
system("cls");
cout<<"211"<<endl;
system("pause");
break;
case 3:
system("cls");
cout<<"333"<<endl;
system("pause");
break;
case 4:
system("cls");
cout<<"433"<<endl;
system("pause");
break;
case 5:
system("cls");
cout<<"555"<<endl;
system("pause");
break;
case 6:
system("cls");
cout<<"666"<<endl;
system("pause");
break;
case 7:
system("cls");
cout<<"777"<<endl;
system("pause");
break;
case 8:
system("cls");
cout<<"888"<<endl;
system("pause");
break;
case 9:
system("cls");
cout<<"999"<<endl;
system("pause");
break;
case 0:
system("cls");
cout<<"再见"<<endl;
system("pause");
exit(0);
}
}
}
展开全部
这样改
#include <stdio.h>
#include <stdlib.h>
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);
}
#include <stdio.h>
#include <stdlib.h>
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |