2012-12-01 45 views
0

这样你就可以采用PET(一种越野车的现在,因为它不会在值读取)C++中它不是从文件中读取回动态结构

在文件中我会看到它转从0变成1;这意味着这是真的。因此应该采用。

这是在bool中读取的正确方法吗?问题是在读的功能,我的问题是,

读>>

#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)); 
} 
+0

请不要咆哮关于EOF。 – user1858740

+0

究竟是什么问题?你能显示一个示例输入文件,预期结果和实际结果吗? – Angew

回答

0

看来问题是在你的逻辑,我已经在你的代码固定的几件事情,它正在一起来看看: 事我固定有: - 注册(动物宠物[],整数&计数) - getNum(INT &计数) - 读取(动物宠物[],整数&计数) - 显示器(动物宠物[], int & count)

#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(); 
}