简体   繁体   English

如何修复“预期的主要表达式”错误

[英]How to fix an "expected primary-expression" error

I'm working on a project in our programming class which requires me to make an inventory system.我正在我们的编程课上做一个项目,它需要我制作一个库存系统。 There is an error in the code in line 243 that gives weird errors when I compile it.第 243 行的代码中有一个错误,当我编译它时会出现奇怪的错误。

Line 243 beneath in the code is代码下方的第 243 行是

while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum而(库存>>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);
}

In find_name() function:find_name()函数中:

while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
           >>rec.prodname>>rec.prodprice>>rec.quantity)

You dont declare what inventory is in this function.您没有在此功能中声明什么inventory Did you mean to open a file in a variable called inventory like you did in the other functions?您是想像在其他函数中那样在名为inventory的变量中打开一个文件吗?

The reason the compiler is giving weird errors is you declared a type called inventory and so not seeing a local variable called inventory it assumes you're using the type which makes no sense in the context of the above line and the compiler gets confused and gives a fairly obscure error.编译器给出奇怪错误的原因是你声明了一个名为inventory的类型,所以没有看到一个名为inventory的局部变量它假设你使用的类型在上面行的上下文中没有意义并且编译器感到困惑并给出一个相当隐晦的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM