每次满足某些条件时,我都需要从包含数据的文本文件中提取和显示记录。问题是当我从文本文件中提取它们时省略了一些记录。任何帮助将不胜感激。我的代码如下,用 Dev-C++ 编写:
#include <iostream>
#include <conio.h>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char manufacturer[16], model[16], year[10];
int miles,car_cost;
char response, line[256];
string field1, field2, field3;
int MilesText ,car_costText;
ofstream OS ("usedcars.txt", ios::out);
cout<<"for each car please enter :"<<endl;
do
{
ofstream OS ("usedcars.txt", ios::app);
cout<<"The manufacturer: ";
cin.getline(manufacturer, 16);
cout<<"The model: ";
cin.getline(model, 16);
cout<<"The year: ";
cin.getline(year, 8);
cout<<"The miles: ";
cin>>miles;
cin.ignore();
cout<<"The cost of car $: ";
cin>>car_cost;
OS<< manufacturer << setw(9) << model << setw(8) << year << setw(11) << miles << setw(8) << car_cost<<endl;
cout<<"Do you want to continue?";
cin>>response;
cin.ignore();
}
while (response!='n');
OS.close();
cout<<"-------------------------------------"<<endl;
cout<<"the record found"<<endl;
ifstream IS ("usedcars.txt", ios::in);
while(!IS.eof())
{
IS>>field1>>field2>>field3>>MilesText>>car_costText;
if(MilesText<50000 && car_costText<9000) //if the miles is less than 50000 and the cost is less than 9000 therefore...
while(IS)
{
IS.getline(line, 256);
if(IS)
cout<<line<<endl; //display the record from text file
}
}
IS.close();
getch();
return 0;
}
**********************输出************************** ******************
for each car please enter :
The manufacturer: Mitsubishi
The model: Lancer
The year: 2001
The miles: 12300
The cost of car $: 10780
Do you want to continue?y
The manufacturer: Ford
The model: Escape
The year: 2004
The miles: 150000
The cost of car $: 6200
Do you want to continue?y
The manufacturer: Audi
The model: A4
The year: 1999
The miles: 79000
The cost of car $: 11000
Do you want to continue?n
找到的记录
*************************在文本文件中************************ ******************
Mitsubishi Lancer 2001 12300 10780
Ford Escape 2004 150000 6200
Audi A4 1999 79000 11000
Volvo S80 1998 14000 7900