いくつかの条件が満たされるたびに、データを含むテキストファイルからレコードを抽出して表示する必要があります。問題は、一部のレコードがテキストファイルから抽出されたときに省略されていることです。どんな助けもありがとう。私のコードは、の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
一方(IS) {IS.getline(ライン256)。 if(IS)は論理を理解していません。あなたは明確にすることができますか? –
なぜ構造体やクラスを使用できないのですか? –
まだファイルにデータがある限り、その行を抽出してください。 – T4000