我正在编程课程中的一个项目,这需要我建立一个库存系统。第243行中的代码中存在错误,在编译时会出现奇怪的错误。
代码下面的第243行是
而(库存>> rec.prodtype>> rec.menutype>> rec.prodnum
#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int x, int y);
void add();
void view();
void menu();
void types();
void input();
void stock();
void find_name();
void find_number();
void find_type();
void find_mtype();
void all();
void output();
struct inventory{
string prodtype,menutype;
string prodnum,prodname;
int quantity;
float prodprice;
}rec;
int main()
{
int x=1,y=1; //for gotoXY
char l; //choice to determine add/view
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t INVENTORY SYSTEM\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\t\t ---- [1] Add new product ----"
<<"\n\t\t ---- [2] Add stocks to existing products ----"
<<"\n\t\t ---- [3] View Products ----"
<<"\n\t\t ---- [4] Exit Program ----"
<<"\n\n\t\t\t\t Enter choice : ";
back:
l=getch();
if(l=='1')
add();
else if(l=='2')
stock();
else if(l=='3')
view();
else if(l=='4')
return 0;
else
goto back;
}
void stock_intro()
{
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t ADDING OF STOCKS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n\n";
}
void stock()
{
string num; //for inputting prodnum...
int x; //for adding stocks
string prodtype,menutype;
string prodnum,prodname;
int quantity;
float prodprice;
ifstream inventory("inventory.txt");
system("cls");
stock_intro();
cout<<"\t\t\t Enter Product Number to add stock:\n\n";
cout<<"\t\t\t ";
cin>>num;
while(inventory>>prodtype>>menutype
>>prodnum>>prodname>>quantity
>>prodprice){
system("cls");
cout<<"How many stocks would you like to add?: ";
cin>>x;
rec.quantity=rec.quantity+x;
}
}
void types()
{
ofstream inventory("inventory.txt", ios::app);
char c;
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tFOOD AND BEVERAGES\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
cout<<"\t\t\t\t [A] SNACKS\n\t\t\t\t [B] CANDIES\n\t\t\t\t [C] BISCUITS\n\t\t\t\t [D] CHOCOLATES\n\t\t\t\t [E] CANNED GOODS\n\t\t\t\t [F] CONDIMENTS\n\t\t\t\t [G] INSTANT NOODLES\n\t\t\t\t [H] BEVERAGE / DRINKS\n";
cout<<"\n\t\t\t\t Enter Choice: ";
back:
c=getch();
c=toupper(c);
if(c=='A')
rec.menutype="SNACKS";
else if(c=='B')
rec.menutype="CANDIES";
else if(c=='C')
rec.menutype="BISCUITS";
else if(c=='D')
rec.menutype="CHOCOLATES";
else if(c=='E')
rec.menutype="CANNED GOODS";
else if(c=='F')
rec.menutype="CONDIMENTS";
else if(c=='G')
rec.menutype="INSTANT NOODLES";
else if(c=='H')
rec.menutype="BEVERAGE / DRINKS";
else
goto back;
inventory<<rec.menutype;
}
void input()
{
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tADDING OF PRODUCTS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n\n";
ofstream inventory("inventory.txt", ios::app);
cout<<"\t\t\tEnter Product number: ";
cin>>rec.prodnum;
cout<<"\t\t\tEnter Product name: ";
cin>>rec.prodname;
cout<<"\t\t\tEnter Product price: ";
cin>>rec.prodprice;
cout<<"\t\t\tEnter Quantity: ";
cin>>rec.quantity;
inventory<<endl
<<rec.prodnum<<endl
<<rec.prodname<<endl
<<rec.prodprice<<endl
<<rec.quantity<<endl<<endl;
}
void add()
{
char c,d; //for choosing food or hygene
char e; //for choosing if add another of not
back2:
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t ADD PRODUCTS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
ofstream inventory ("inventory.txt", ios::app);
cout<<"\n\n\t\t\t\t[A] FOOD and BEVERAGES\n\t\t\t\t[B] HYGIENE";
cout<<"\n\n\t\t\t\tEnter Choice: ";
back1:
c=getch();
d=toupper(c);
if(d=='A')
rec.prodtype="Food & Beverages";
else if(d=='B')
rec.prodtype="Hygiene";
else
goto back1;
system("cls");
inventory<<rec.prodtype<<endl;
types();
input();
cout<<"\n\t\t\tEnter another product? [Y / N]: ";
back3:
e=getch();
e=toupper(e);
if(e=='Y')
goto back2;
else if(e=='N')
main();
else
goto back3;
}
void output()
{
cout<<"Product Type: "
<<rec.prodtype<<endl;
cout<<"Menu Type : "
<<rec.menutype<<endl;
cout<<" Product Number: "
<<rec.prodnum<<endl;
cout<<" Product Name: "
<<rec.prodname<<endl;
cout<<" Product Price: "
<<rec.prodprice<<endl;
cout<<" Stocks Available: "
<<rec.quantity<<endl;
}
void view_intro()
{
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tVIEWING OF INVENTORY\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
}
void view()
{
char ch;
system("cls");
view_intro();
cout<<"\n\t\t ---- [1] Find by Product Name ----"
<<"\n\t\t ---- [2] Find by Product Number ----"
<<"\n\t\t ---- [3] Find by Product Type ----"
<<"\n\t\t ---- [4] Find by Menu Type ----"
<<"\n\t\t ---- [5] Display all Records ----"
<<"\n\n\t\t\t\t Enter choice : ";
back:
ch=getch();
ch=getch();
switch(ch)
{
case '1': find_name();break;
case '2': find_number();break;
case '3': find_type();break;
case '4': find_mtype();break;
case '5': all();break;
default: goto back;
}
}
void find_name()
{
string name;
int v=0;
system("cls");
view_intro();
ifstream student("record.txt");
cout<<"\n Enter Product Name [incorrect capitalization is invalid]: ";
cin>>name;
system("cls");
view_intro();
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
>>rec.prodname>>rec.prodprice>>rec.quantity)
{
back:
while(name==rec.prodname)
{
output();
break;
}
v++;
if(v<1)
{
goto back;
}
}
system("pause");
main();
}
void gotoXY(int x, int y)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}
答案 0 :(得分:1)
在find_name()
函数中:
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
>>rec.prodname>>rec.prodprice>>rec.quantity)
你没有声明这个函数中inventory
是什么。您的意思是像在其他函数中一样在名为inventory
的变量中打开文件吗?
编译器出现奇怪错误的原因是你声明了一个名为inventory
的类型,因此没有看到一个名为inventory的局部变量,它假定你使用的类型在上下文中是没有意义的。上面的行和编译器混淆并给出一个相当模糊的错误。