c++ - 文件指针只读取第一个条目
问题描述
今天是个好日子。我的程序是关于计算机商店(笔记本电脑)的。所以,人们可以通过这个程序购买笔记本电脑,而“PCPurchase”就是处理客户交易的功能。
我使用文件指针和结构来保存有关所有笔记本电脑的信息。当然,struct 存储不止一种类型的笔记本电脑,因为显然这是一家笔记本电脑商店。
我遇到了一个问题,用户只能购买(输入名称)结构中的第一个条目(第一台笔记本电脑)。这里:cout << "Enter the laptop company and name you want to buy: " << endl;
如果客户输入第 2 台笔记本电脑的名称,以此类推,会跳到行
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 语句。如果要从数据的第二条记录购买笔记本电脑,以此类推,它将与第一条记录进行比较,并且不会返回true。因此,它将继续执行 else 语句,并且不会重复 while 循环。只需更改 else 语句即可使循环正常工作。
推荐阅读
- angular - 达到限制时如何聚焦下一个输入但如果输入嵌套在角度的Html li标签中?
- instapy - “无法检测帖子媒体类型。跳过”InstaPy 机器人不喜欢帖子
- python - 如何绘制分支 sin(x) 函数
- android-studio - 勾选记住密码时Android Studio“无法恢复密钥”
- javascript - 调用 setState 后 useState setState 不更新状态
- azure - 为什么 docker login 将我的密码存储在未加密的文件夹中,我应该对此做些什么?
- android-studio - 如何使用 Volley 将 Object 列表转换为 MutableList
- react-native - 如何禁用 RCTView?
- vcf-vcard - 将照片添加到 vCard
- javascript - 如何将对象的属性分配给reactjs中的另一个属性?