如何修复"expected primary-expression"错误

How to fix an "expected primary-expression" error

本文关键字:错误 primary-expression expected 何修复      更新时间:2023-10-16

我正在编程课上做一个项目,需要我做一个库存系统。在第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<<"nnÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    cout<<"tttt  INVENTORY SYSTEMn";
    cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    cout<<"ntt   ---- [1]  Add new product           ----"
        <<"ntt   ---- [2]  Add stocks to existing products   ----"
        <<"ntt   ---- [3]  View Products         ----"
        <<"ntt   ---- [4]  Exit Program          ----"
        <<"nntttt  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<<"nnÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    cout<<"tttt   ADDING OF STOCKSn";
    cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍnn";
}
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<<"ttt     Enter Product Number to add stock:nn";
    cout<<"ttt     ";
    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<<"nnÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    cout<<"ttttFOOD AND BEVERAGESn";
    cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍn";
    cout<<"tttt [A] SNACKSntttt [B] CANDIESntttt [C] BISCUITSntttt [D] CHOCOLATESntttt [E] CANNED GOODSntttt [F] CONDIMENTSntttt [G] INSTANT NOODLESntttt [H] BEVERAGE / DRINKSn";
    cout<<"ntttt 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<<"nnÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    cout<<"ttttADDING OF PRODUCTSn";
    cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍnn";
    ofstream inventory("inventory.txt", ios::app);
    cout<<"tttEnter Product number: ";
    cin>>rec.prodnum;
    cout<<"tttEnter Product name: ";
    cin>>rec.prodname;
    cout<<"tttEnter Product price: ";
    cin>>rec.prodprice;
    cout<<"tttEnter 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<<"nnÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    cout<<"tttt     ADD PRODUCTSn";
    cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    ofstream inventory ("inventory.txt", ios::app);
    cout<<"nntttt[A] FOOD and BEVERAGESntttt[B] HYGIENE";
    cout<<"nnttttEnter 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<<"ntttEnter 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<<"nnÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
    cout<<"ttttVIEWING OF INVENTORYn";
    cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍn";
}
void view()
{
    char ch;
    system("cls");
    view_intro();
    cout<<"ntt        ---- [1]  Find by Product Name   ----"
        <<"ntt        ---- [2]  Find by Product Number  ----"
        <<"ntt        ---- [3]  Find by Product Type   ----"
        <<"ntt        ---- [4]  Find by Menu Type      ----"
        <<"ntt        ---- [5]  Display all Records     ----"
        <<"nntttt  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);
}

find_name()函数:

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

你没有在这个函数中声明inventory是什么。你的意思是在一个名为inventory的变量中打开一个文件,就像你在其他函数中做的那样?

编译器给出奇怪错误的原因是您声明了一种称为inventory的类型,因此没有看到称为inventory的局部变量,它假设您正在使用在上面一行上下文中没有意义的类型,编译器会感到困惑,并给出一个相当模糊的错误。