文件指针只读取第一个条目

File pointer only read the first entry

大家好。我的程序是关于一家电脑商店(笔记本电脑)的。因此,人们可以通过此程序购买笔记本电脑,"PCPurchase" 是处理客户交易的功能。

我使用文件指针和结构来保存所有笔记本电脑的信息。当然,该结构存储不止一种类型的笔记本电脑,因为显然这是一家笔记本电脑商店。

我遇到一个问题,用户只能购买(输入名称)结构中的第一个条目(第一台笔记本电脑)。这里:cout << "Enter the laptop company and name you want to buy: " << endl;

如果客户输入第 2 台笔记本电脑的名称,第 3 台等等,它会跳转到行 cout << endl << "\tNot available!" << endl; cout << "\tPress A to try again or B to return to main menu" << endl; 这表明笔记本电脑的名称不在数据库中,而实际上是。

我能知道这里的问题到底是什么吗?

int PCPurchase()
{
    struct customer cust;
    system("color 0A");
    char laptop[100];
    double total_bill;
    const double TAX=0.06;

    system("cls");
    cout << setfill ('-') << setw (55) << "-" << endl;
    cout << "\t\tCustomer Dashboard" << endl;
    cout << setfill ('-') << setw (55) << "-" << endl;

    fptr=fopen("laptop.txt","ab+");
    cout << "Available laptops: " << endl;
    rewind(fptr);
    while(fread(&PC,sizeof(PC),1,fptr)==1)
    {
        cout << endl << "Laptop company and name: ";
        cout << PC.laptopcompany << endl;
        cout << "RAM: ";
        cout << PC.RAM << endl;
        cout << "Processor: ";
        cout << PC.Processor << endl;
        cout << "Price: RM";
        cout << PC.price << endl;
    }
    cout << "\nPress any key to continue purchase" << endl;
    getch();
    fflush(stdin);

    getInfo(cust); //get information of customer

    cout << "Enter the laptop company and name you want to buy: " << endl;
    cout << "(Type 'RETURN' if you do not want to purchase)" << endl << endl;
    gets(laptop);
    rewind(fptr);
    while(fread(&PC,sizeof(PC),1,fptr)==1)
    {  
        if(strcmpi(PC.laptopcompany,laptop)==0)
        {
            cout << setfill ('-') << setw (55) << "-" << endl;
            cout << "\tYou have selected" << endl;
            cout << setfill ('-') << setw (55) << "-" << endl;
            cout << "Laptop company and name: ";
            cout << PC.laptopcompany << endl;
            cout << "RAM: ";
            cout << PC.RAM << endl;
            cout << "Processor: ";
            cout << PC.Processor << endl;
            cout << "Price: ";
            cout << PC.price << endl;

            total_bill=PC.price+(PC.price*TAX);

            cout << setfill ('-') << setw (55) << "-" << endl;
            cout << fixed << showpoint << setprecision (2);
            cout << "Name: "<< cust.name << endl; // struct output
            cout << "Email: "<< cust.email << endl;
            cout << "Phone Number: " << cust.number << endl;
            cout << "Your total bill (including 6% tax): RM" << total_bill << endl;
            cout << setfill ('-') << setw (55) << "-" << endl;

            cout << endl << "\tPress 1 to return to main screen!";
            cout << endl << "\tPress 2 to quit the program!";

            char afterpurchase;
            afterpurchase=getche();

            if (afterpurchase=='1')
            {
                fclose(fptr);
                main();
            }
            else
                exit_system();
        }
        else if(strcmpi("RETURN",laptop)==0)
            main();
        else
        {
            cout << endl << "\tNot available!" << endl;
            cout << "\tPress A to try again or B to return to main menu" << endl;
            char choice1;
            choice1=getche();
            choice1=toupper(choice1); // Transform to uppercase

            switch (choice1)
            {
                case 'A': fclose(fptr);
                          PCPurchase();
                          break;

                default : fclose(fptr);
                          main();
                          break;
            }
        }
    }
}

是因为else语句。如果要从第二条记录的数据开始买笔记本电脑等等,会和第一条记录比较,不会return为真。因此,它将继续执行 else 语句并且不会重复 while 循环。只需更改 else 语句即可使循环正常工作。