文件指针仅读取第一项

时间:2018-12-11 16:48:08

标签: c++ struct file-pointer

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

我使用文件指针和结构来保存有关所有便携式计算机的信息。当然,该结构存储了超过一种类型的笔记本电脑,因为显然这是一家笔记本电脑商店。

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

如果客户输入第二台笔记本电脑,第三台笔记本电脑等的名称,它将跳到第 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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是因为else语句。如果要从数据的第二条记录中购买笔记本电脑,依此类推,它将与第一条记录进行比较,并且不会返回true。因此,它将继续进行else语句,并且不会重复while循环。只需更改else语句以使循环正常工作即可。