1

いくつかの条件が満たされるたびに、データを含むテキスト ファイルからレコードを抽出して表示する必要があります。問題は、テキスト ファイルからレコードを抽出すると、一部のレコードが省略されることです。どんな助けでも大歓迎です。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
4

2 に答える 2

1

少なくとも、あなたが正しくやりたいことを理解していれば、あなたの問題(入力ファイルを読み取る部分)のいくつかの解決策があります。私の場合、入力ファイルから、走行距離が50000未満で、価格が9000未満のすべての車を印刷します。

#include <sstream>
// rest of your code...

ifstream IS ("usedcars.txt", ios::in);
string lineTextfile;

while(IS)
{   
    getline(IS, lineTextfile); // read one line of input file

    istringstream parseLine(lineTextfile);

    parseLine>>field1>>field2>>field3>>MilesText>>car_costText; // parse individual elements of that line

    if(MilesText<50000 && car_costText<9000)  //if the miles is less than 50000 and        the cost  is less than 9000 therefore...
    {
        cout<<lineTextfile<<endl;  //display the record from text file
    }
}

IS.close();

ファイルストリームと文字列ストリームからのデータの読み取りはやや冗長ですが、最初の例に近いものです。


を使用しstructたい場合は、このコードを使用して、次のようなストリーム演算子をオーバーロードできます。

struct Car
{
    Car() : year(0), mileage(0), price(0) {};
    string manufacturer;
    string model;
    unsigned int year;
    unsigned int mileage;
    unsigned int price;
};

istream& operator>>(istream& is, Car& car)
{
    is >> car.manufacturer >> car.model >>  car.year >>  car.mileage >>  car.price;
    return is;
}

ostream& operator<<(ostream& os, const Car& car)
{
    os << car.manufacturer << " " << car.model << " " <<  car.year << " " <<  car.mileage << " " <<  car.price;
    return os;
}

bool IsFairPricedCar(const Car& car)
{
    return car.price < 9000 && car.mileage < 50000;
}

そのstruct定義により、次のことが可能になります。

ifstream IS ("usedcars.txt", ios::in);

while (IS)
{
    Car readCar;
    IS >> readCar;
    if(readCar.mileage < 50000 && readCar.price < 9000)
    {
        cout << readCar << endl;
    }
}

IS.close();

代替案

ifstream IS2 ("usedcars.txt", ios::in);

copy_if(istream_iterator<Car>(IS2), istream_iterator<Car>(),
    ostream_iterator<Car>(cout, "\n"), IsFairPricedCar);
于 2012-03-16T10:56:22.300 に答える
0

あなたはあなたの車の特性を読み、それらが一致する場合は次の行を印刷するようです。確認したいもう1つのことは、書き込む値にスペースが含まれていないことです。値を書き込むときは、値の間に少なくとも1つのスペースがあることを確認する必要があります。

ループ内の条件に使用eof()していることに気付きました。これは間違いなく間違っており、への暗黙の変換のみを使用したいと考えていますbool。の唯一の使用法はeof()、ファイルの終わりに達したために読み取りに失敗した場合のエラーレポートを抑制することです。

于 2012-03-16T10:57:04.583 に答える