C++ C++;它';它没有从文件读回动态结构

C++ C++;它';它没有从文件读回动态结构,c++,C++,所以你可以领养一只宠物(现在有点像马车,因为它没有读入值) 在文件中,我将看到它从0变为1;这意味着这是真的。所以应该说是通过了 这是正确的阅读方式吗?问题在于read函数,我的问题是 阅读>> #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; const int TEMP_SIZE = 250; st

所以你可以领养一只宠物(现在有点像马车,因为它没有读入值)

在文件中,我将看到它从0变为1;这意味着这是真的。所以应该说是通过了

这是正确的阅读方式吗?问题在于read函数,我的问题是

阅读>>

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

const int TEMP_SIZE = 250;

struct animal
{
    char *name;
    char *breed;
    float age;
    float weight;
    char *desc;
    bool adopted;
//we generate this
    int ID; 
};

struct family
{
    char *host;
    char *address;
    int numDogs;    
};

class petAdoption
{
    public:
    //petAdoption();
    //~petAdoption();
    void enroll(animal pet[], int & count);
    void read(animal pet[], int & count);
    void display(animal pet[], int & count);
    void adoptPet(animal pet[], int & count);
    void getNum(int & count);
};

void petAdoption::enroll(animal pet[], int & count)
{
//name Dynamic array 1;
    char temp[TEMP_SIZE];
    cout << "Please your pet's name: ";
    cin.get(temp, TEMP_SIZE);
    cin.ignore(100, '\n');
    pet[count-1].name = new char[strlen(temp)];
    strcpy(pet[count-1].name, temp);
//breed Dynamic array 2;
    cout << "Please your pet's breed: ";
    cin.get(temp, TEMP_SIZE);
    cin.ignore(100, '\n');
    pet[count-1].breed = new char[strlen(temp)];
    strcpy(pet[count-1].breed, temp);
//desc Dynamic array 3;
    cout << "Please a desc of your pet: ";
    cin.get(temp, TEMP_SIZE);
    cin.ignore(100, '\n');
    pet[count-1].desc = new char[strlen(temp)];
    strcpy(pet[count-1].desc, temp);
//not adopted 
    pet[count-1].adopted = false;
    ofstream write;

    write.open("pets.txt", ios::app);
        write << pet[count-1].name << '\n';
    write.close();
    write.open(pet[count-1].name);
        write << pet[count-1].name << '\n'
        << pet[count-1].breed << '\n'
        << pet[count-1].desc << '\n'
        << pet[count-1].adopted << '\n';
    write.close();
}


//This method basically, allocates memory for the array.
void petAdoption::getNum(int & count)
{
    ifstream read;  
    char temp[TEMP_SIZE];
    read.open("pets.txt");
        if(!read)
        {
            cout << "error 1" << endl;
            count = 1;
        }
        else{
            while(!read.eof())
            {
                read.getline(temp, TEMP_SIZE);
                count = ++count;
            }
        }
    read.close();
}

void petAdoption::read(animal pet[], int & count)
{
    ifstream read;  
    char temp[TEMP_SIZE];

//read in the names
    int k = 0;
    read.open("pets.txt");
    if(!read)
    {
        cout << "There's No pets.txt file! (Ignore this!)" <<endl;
    }
    else 
    {
        while(!read.eof())
        {
            read.getline(temp, TEMP_SIZE);
            pet[k].name = new char[strlen(temp)+1];
            strcpy(pet[k].name, temp);
        ++k;
        }
    }
    read.close();




    for (int i = 0; i < count-1; ++i)
    {
        read.open(pet[i].name);
            if(!read)
            {
                cout << "error 2" << endl;
            }
            else{
                while(!read.eof())
                {
        //name
                    read.getline(temp, TEMP_SIZE);
                    pet[i].name = new char[strlen(temp)+1];
                    strcpy(pet[i].name, temp);
        //breed
                    read.getline(temp, TEMP_SIZE);
                    pet[i].breed = new char[strlen(temp)+1];
                    strcpy(pet[i].breed, temp);
        //desc
                    read.getline(temp, TEMP_SIZE);
                    read.ignore(100, '\n');
                    pet[i].desc = new char[strlen(temp)+1];
                    strcpy(pet[i].desc, temp);

                    read >> pet[i].adopted;
                cout << i << endl;
                }
            }
        read.close();
    }
}

void petAdoption::display(animal pet[], int & count)
{
    for (int i = 0; i < count-1; ++i){
        cout << "Pet id = " << i << '\n' << endl;
        cout << pet[i].name << '\n' << pet[i].breed << '\n' << pet[i].desc << '\n';
        cout << pet[i].adopted << endl;
        if (pet[i].adopted == false){
            cout << "Not adopted" << '\n' << endl;
        }
        if (pet[i].adopted == true){
            cout << "Adopted" << '\n' << endl;
        }
    }
}

void petAdoption::adoptPet(animal pet[], int & count)
{   
    int adoptID;
    cout << "Which pet would you like to adopt? (Please enter the ID, EG: 2 or 5): ";
    cin >> adoptID;
    cin.ignore(100, '\n');

    pet[adoptID].adopted = true;
    cout << pet[adoptID].adopted << endl;

    ofstream write;
    for (int i = 0; i < count; ++i){
        write.open(pet[i].name);
            write << pet[i].name << '\n'
            << pet[i].breed << '\n'
            << pet[i].desc << '\n'
            << pet[i].adopted << '\n';
        write.close();
    }
}

int main()
{
//When starting the program, we read in from the text files first
//and then we count how many animals we have had in the shelter
//and we use that as a base to dynamically allocate the structs 
    petAdoption adopt;
    char again;
    int choice;
    do {
        int count = 0;
        adopt.getNum(count);
        animal *pet;
        pet = new animal[count];
        adopt.read(pet, count);
        cout << "~~~~~~~~~~~~~~~~~~MENU~~~~~~~~~~~~~~~~~~" << endl;
        cout << "1) Enroll a pet" << endl;
        cout << "2) Display pets" << endl;
        cout << "3) Adopt a pet" << endl;
        cout << "Please make your selection (1-3): ";
        cin >> choice;
        cin.ignore(100,'\n');
        if (1 == choice){
            adopt.read(pet, count);
            adopt.enroll(pet, count);
        }
        if (2 == choice){
            adopt.read(pet, count);
            adopt.display(pet, count);
        }
        if (3 == choice){
            adopt.read(pet, count);
            adopt.adoptPet(pet, count);
        }
        cout << "Would you like to try again? (y/n): ";
        cin >> again;
        cin.ignore(100,'\n');
    } while ('Y' == toupper(again)); 
}
#包括
#包括
#包括
#包括
使用名称空间std;
const int TEMP_SIZE=250;
结构动物
{
字符*名称;
char*品种;
浮动年龄;
浮重;
char*desc;
布尔通过;
//我们生成了这个
int-ID;
};
结构族
{
字符*主机;
字符*地址;
int numDogs;
};
类掺杂
{
公众:
//petAdoption();
//~petAdoption();
无效登记(动物宠物[],整数和计数);
无效读取(动物宠物[],整数和计数);
无效显示(动物宠物[],整数和计数);
无效领养宠物(动物宠物[],整数和计数);
void getNum(int和count);
};
void petAdoption::登记(动物宠物[],整数和计数)
{
//名称动态数组1;
字符温度[温度大小];

cout问题似乎出在您的逻辑中,我已经在您的代码中修复了一些东西,并且正在运行。请看: 我解决的问题是: -登记(动物宠物[],整数和计数) -getNum(整数和计数) -读取(动物宠物[],整数和计数) -显示(动物宠物[],整数和计数)

#包括
#包括
#包括
#包括
使用名称空间std;
const int TEMP_SIZE=250;
结构动物
{
字符*名称;
char*品种;
浮动年龄;
浮重;
char*desc;
布尔通过;
//我们生成了这个
int-ID;
};
结构族
{
字符*主机;
字符*地址;
int numDogs;
};
类掺杂
{
公众:
//petAdoption();
//~petAdoption();
无效登记(动物宠物[],整数和计数);
无效读取(动物宠物[],整数和计数);
无效显示(动物宠物[],整数和计数);
无效领养宠物(动物宠物[],整数和计数);
void getNum(int和count);
};
void petAdoption::登记(动物宠物[],整数和计数)
{
//名称动态数组1;
字符温度[温度大小];

请不要抱怨EOF。到底是什么问题?您能展示一个示例输入文件、预期结果和实际结果吗?
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

const int TEMP_SIZE = 250;

struct animal
{
    char *name;
    char *breed;
    float age;
    float weight;
    char *desc;
    bool adopted;
//we generate this
    int ID; 
};

struct family
{
    char *host;
    char *address;
    int numDogs;    
};

class petAdoption
{
    public:
    //petAdoption();
    //~petAdoption();
    void enroll(animal pet[], int & count);
    void read(animal pet[], int & count);
    void display(animal pet[], int & count);
    void adoptPet(animal pet[], int & count);
    void getNum(int & count);
};

void petAdoption::enroll(animal pet[], int & count)
{
//name Dynamic array 1;
    char temp[TEMP_SIZE];
    cout << "Please your pet's name: ";
    cin.get(temp, TEMP_SIZE);
    cin.ignore(100, '\n');
    pet[count-1].name = new char[strlen(temp)];
    strcpy(pet[count-1].name, temp);
//breed Dynamic array 2;
    cout << "Please your pet's breed: ";
    cin.get(temp, TEMP_SIZE);
    cin.ignore(100, '\n');
    pet[count-1].breed = new char[strlen(temp)];
    strcpy(pet[count-1].breed, temp);
//desc Dynamic array 3;
    cout << "Please a desc of your pet: ";
    cin.get(temp, TEMP_SIZE);
    cin.ignore(100, '\n');
    pet[count-1].desc = new char[strlen(temp)];
    strcpy(pet[count-1].desc, temp);
//not adopted 
    pet[count-1].adopted = false;
    ofstream write;

    write.open("pets.txt", ios::beg | ios::app);
    write << pet[count-1].name << '\n';
    write << pet[count-1].breed << '\n';
    write << pet[count-1].desc << '\n';
    write << pet[count-1].adopted << '\n';
    write.close();
}


//This method basically, allocates memory for the array.
void petAdoption::getNum(int & count)
{
    ifstream read;  
    char temp[TEMP_SIZE];
    int count_temp = 0;
    read.open("pets.txt");

    while(!read.eof())
    {
        read.getline(temp, TEMP_SIZE);
        count_temp++;
        if (count_temp == 4){
           count++;
           count_temp = 0;
        }
    }

    read.close();
}

void petAdoption::read(animal pet[], int & count)
{
    ifstream read;  
    char temp[TEMP_SIZE];

//read in the names
    int k = 0, i =0;
    read.open("pets.txt");

    while(read.good()){
        if (i < count){
    //name
            read.getline(temp, TEMP_SIZE);
            pet[i].name = new char[strlen(temp)+1];
            strcpy(pet[i].name, temp);
    //breed
            read.getline(temp, TEMP_SIZE);
            pet[i].breed = new char[strlen(temp)+1];
            strcpy(pet[i].breed, temp);
    //desc
            read.getline(temp, TEMP_SIZE);
            pet[i].desc = new char[strlen(temp)+1];
            strcpy(pet[i].desc, temp);

            read.getline(temp, TEMP_SIZE);
            if (strcmp(temp,"0") == 0){
                pet[i].adopted = true;
            }else{
                pet[i].adopted = false;
            }
        }else{
          cout << i << endl;
          break;
        }

        i++;
    }

        read.close();

}

void petAdoption::display(animal pet[], int & count)
{
    for (int i = 0; i < count; i++){
        cout << "Pet id = " << i << '\n' << endl;
        cout << pet[i].name << '\n' << pet[i].breed << '\n' << pet[i].desc << '\n';
        cout << pet[i].adopted << endl;
        if (pet[i].adopted == false){
            cout << "Not adopted" << '\n' << endl;
        }else{
            cout << "Adopted" << '\n' << endl;
        }
    }
}

void petAdoption::adoptPet(animal pet[], int & count)
{   
    int adoptID;
    cout << "Which pet would you like to adopt? (Please enter the ID, EG: 2 or 5): ";
    cin >> adoptID;
    cin.ignore(100, '\n');

    pet[adoptID].adopted = true;
    cout << pet[adoptID].adopted << endl;

    ofstream write;
    write.open("pets.txt", ios::beg);

    for (int i = 0; i < count; ++i){
            write << pet[i].name << '\n'
            << pet[i].breed << '\n'
            << pet[i].desc << '\n'
            << pet[i].adopted<<'\n';
    }
        write.close();
}