2012-12-01 73 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(); 
}