-2

私はいくつかのものを考え出しましたが、私はfstreamsを吸っています.BREED + DESCの場合、それらと名前の間にスペースがありますが、完全に削除するとスペースがなくなり、再び動作します. 誰かが私が間違っていることを教えてもらえますか?

#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;


const int NAME_SIZE = 100;
const int BREED_SIZE = 100;
const int DESC_SIZE = 250;
const int REASON_SIZE = 250;

//creating the struct
struct animal
{
//members
    char name[NAME_SIZE];
    char breed[BREED_SIZE];
    char desc[DESC_SIZE];
    char reason[REASON_SIZE];
    float age;
    float weight;
    int day;
    int month;
    int year;
    float length;
};

class petAdoption
{
    public:
        petAdoption();
        void enroll(animal newAnimal[], int & count);
        void read(animal newAnimal[], int & count);
        //void display(animal & newAnimal);
        //void adopt(animal & newAnimal); 
    private:


};
petAdoption::petAdoption()
{

}

int main()
{
int count = 0;
animal * newAnimal = new animal[count];
petAdoption adopt;
adopt.read(newAnimal, count);
adopt.enroll(newAnimal, count);
delete[] newAnimal;
}


void petAdoption::read(animal newAnimal[], int & count)
{
    int pets = 0;
    ifstream read;
    read.open("pets.txt");
    if(!read)
    {
        cout << "Checking... File doesn't exist!" <<endl;
    }
    else 
    {
        while(!read.eof())
        {
        read.getline(newAnimal[pets].name, NAME_SIZE, '\n');
        read.ignore(100, '\n');
        ++pets;
        }
    count = pets + 1;
    }
    read.close();

    for (int i = 0; i < pets; ++i){
        read.open(newAnimal[i].name);
            if( !read)
            {
                cout << "Checking... File doesn't exist!" <<endl;
            }
        else{
            while(!read.eof())
            {
                read.getline(newAnimal[i].name, NAME_SIZE, '\n');
                read.getline(newAnimal[i].breed, BREED_SIZE, '\n');
                read.getline(newAnimal[i].desc, DESC_SIZE, '\n');
                read.getline(newAnimal[i].reason, REASON_SIZE, '\n');
                read.ignore(100, '\n');
                read >> newAnimal[i].age;
                read >> newAnimal[i].weight;
                read >> newAnimal[i].day;
                read >> newAnimal[i].month;
                read >> newAnimal[i].year;
                read >> newAnimal[i].length;
                read.ignore(100, '\n');
            }
        }
        read.close();
    }
}


/*
ENROLL FUNCTION
*/

void petAdoption::enroll(animal newAnimal[], int & count)
{
//making a write variable
    ofstream write;

//stores the name into the struct member
    cout << "Please enter a name: ";
    cin.get(newAnimal[count].name, NAME_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter a breed: ";
    cin.get(newAnimal[count].breed, BREED_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter a desc: ";
    cin.get(newAnimal[count].desc, DESC_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter a reason (EG: eats people): ";
    cin.get(newAnimal[count].reason, REASON_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter an age (EG: 5): ";
    cin >> newAnimal[count].age;
    cin.ignore(100, '\n');
    cout << "Please enter a weight (in LBS): ";
    cin >> newAnimal[count].weight;
    cin.ignore(100, '\n');
    cout << "Please enter a day (EG: 2): ";
    cin >> newAnimal[count].day;
    cout << "Please enter a month (EG: 11): ";
    cin >> newAnimal[count].month;
    cout << "Please enter a year (EG: 2002): ";
    cin >> newAnimal[count].year;
    cout << "How long has the pet been in a shelter? (in months): ";
    cin >> newAnimal[count].length;


//error
    if(!write)
    {
        cout << "Invalid File" << endl;
    }
    else{
        write.open(newAnimal[count].name, ios::app);    
        write << newAnimal[count].name << '\n'
        << newAnimal[count].breed << '\n'
        << newAnimal[count].desc << '\n'
        << newAnimal[count].reason << '\n'
        << newAnimal[count].age << '\n'
        << newAnimal[count].weight << '\n'
        << newAnimal[count].day << '\n'
        << newAnimal[count].month << '\n'
        << newAnimal[count].year << '\n'
        << newAnimal[count].length << '\n';
        write.close();
    }
    if(!write) 
    {
        cout << "Invalid File";
    }
    else {
        write.open("pets.txt", ios::app);
        write << newAnimal[count].name << '\n';
        write.close();  
    }               
}
4

1 に答える 1

1

人々が言及しているように(この質問の以前のバージョンへの回答として)eof()、 loop を制御するために使用しないでください。さらに、あなたはそれを読んだ[試みた]後、あなたの入力を正常に受け取ったことをテストしなければなりません。

get()行がむしろの使用によるものである場合、最後に余分な改行がある問題getline():前者は停止文字を保持しますが、後者は保持しません。std::stringと一緒に使用することも検討する必要がありstd::getline()ます。任意の長さの文字列を読み取ります。

于 2012-11-28T09:59:49.483 に答える