一个简单的商品管理系统,在比较字符串是卡住了(就是商品的名字),
以下代码问题出在,只能修改第一个商品的价格和库存,其他的无法修改
能帮忙看看吗
[code=C/C++][/code]/*
我想用string声名一个字符串 平时用的是<iostream.h>. 发现string类
不能再他里面使用。下面是简单的说明
#include<string.h>
是C语言的字符数组库函数,不是C++的string类
C++的string类只能用:
#include<string>
using namespace std;
如果C字符数组头文件要这样用,必须改为:
#include <cstring>
using namespace std;
原因在于C的标准出来得早,C++ 的string类是后来的
#define N 20
define只是简单替换,而不是作为一个量来使用.与int N=20;是有区别的。
*/
#include<iostream.h>
#include<string.h>
#define size 20
struct product
{
char name[size]; //考虑为啥再此不能用string name;保存字符串
float price;
char grade; //商品等级用单个大写字母表示
int belong; //商品所属柜台暂时用数字表示
int storage;
};
//定义全局变量
int N=20,real__length=0; //N为数组长度上限,real__length为数组实际长度 ,是商品实际数量
product list[20]; //声名全局数组时,数组长度必须是明确的,list[N]是错误的
char name[size];
void product__put__in();
void price__adjust();
void storage__adjust();
void list__cancle();
void product__put__out();
int main()
{
cout<<"商品管理系统的主菜单:"<<endl;
cout<<"1---商品进库"<<endl;
cout<<"2---商品出库"<<endl;
cout<<"3---商品调价"<<endl;
cout<<"4---更改商品库存"<<endl;
cout<<"5---删除商品目录"<<endl;
cout<<"0---终止操作"<<endl<<endl;
cout<<"现在请您选择操作:";
int input;
while(cin>>input&&input!=0)
{
switch(input)
{
case 1:
product__put__in();
break;
case 2:
product__put__out();
break;
case 3:
price__adjust();
break;
case 4:
storage__adjust();
break;
case 5:
list__cancle();
break;
default:
break;
}
cout<<"\n现在请您选择操作:";
}
return 0;
}
void product__put__in()
{
cout<<"请输入商品名称:";
cin>>list[real__length].name;
cout<<"请输入商品价格:";
cin>>list[real__length].price;
cout<<"请输入商品质量级别等级:";
cin>>list[real__length].grade;
cout<<"请输入商品所属柜台管理:";
cin>>list[real__length].belong;
cout<<"请输入商品的库存:";
cin>>list[real__length].storage;
real__length++;
cout<<endl;
}
// 商品调价函数
void price__adjust()
{
cout<<"请输入需要调价商品的名称:";
cin>>name;
for(int i=0;i<real__length;i++)
{
if(!(strcmp(list[i].name,name)==0)) //如果写成list[i].name==name,就错了!! 因为它们的值是地址,
{ //每个数据的地址只有一个,当然不相等了.我在这里调试了半天
cout<<"商品的价格改为:";
cin>>list[i].price;
break; //break是必要的
} //list[i].price=price;
}
}
//更改商品库存
void storage__adjust()
{
cout<<"请输入需要更改库存的商品的名称:";
cin>>name;
for(int i=0;i<real__length;i++)
{
if(!(strcmp(list[i].name,name)==0))
{
cout<<"库存量改为:";
cin>>list[i].storage;
break;
}
}
}
//增加或删除商品目录用两个函数实现
//删除商品目录
void list__cancle()
{
cout<<"请输入需要删除商品的名称:";
cin>>name;
for(int i=0;i<real__length;i++)
{
if(list[i].name==name)
{
for(int j=i;j<real__length-1;j++)
list[j]=list[j+1]; //如果list[i]需要删除,
//就使list[i+1]覆盖list[i],list[i+2]覆盖list[i+1],以此类推
real__length--;
}
}
}
//输出库存
void product__put__out()
{
int len;
len=real__length;
cout<<"*********************************************************"<<endl;
for(int i=0;i<len;i++)
{
cout<<"商品名称\t商品价格\t 商品质量等级 \t商品所属柜台管理\t商品库存"<<endl;
cout<<list[i].name<<" \t"<<list[i].price<<"\t\t"<<list[i].grade<<"\t\t"<<list[i].belong<<"\t\t"<<list[i].storage<<endl;
}
}
[解决办法]
单步调式吧。
C语言里面比较两个字符串,通常用strcmp这个函数。
如果用stl中的string的话,那么可以直接用“==”
[解决办法]
为啥调价和更改库存 你要在IF里面加一个!取反? 直接判断strcmp()==0为真还是为假呀
if(!(strcmp(list[i].name,name)==0))
[解决办法]
++
要么 if( strcmp(list[i].name,name)==0 )
要么 if( !(strcmp(list[i].name,name)) )