2012-11-28 87 views
0

好的,所以當我開始使用類來創建函數時,它給了我一個錯誤文件是無效的,通常不會,如果我不使用類。如果你運行它,輸入一些信息,然後再次運行它,通常它應該正確讀入數據,它仍然讀入,但它說有錯誤。我真的不知道我做錯了什麼,有什麼提示?謝謝!C++ FSTREAM&classes

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

const int TEMP_SIZE = 10; 
const int NAME_SIZE = 100; 
const int BREED_SIZE = 100; 
const int DESC_SIZE = 250; 
const int REASON_SIZE = 250; 
const int ID_SIZE = 50; 

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

struct adopted 
{ 
    char hostName[100]; 
    char hostAddress[100]; 
    int numPets; 
}; 

class petAdoption 
{ 
    public: 
     petAdoption(); 
     //~petAdoption(); 
     void enroll(animal newAnimal[]); 
     void read(animal newAnimal[]); 
    private: 
     int count; 
     int numPets; 
     int * pets; 
}; 

petAdoption::petAdoption() 
{ 
    count = 0; 
    pets = NULL; 
    numPets = 0; 
} 

void petAdoption::enroll(animal newAnimal[]) 
{ 
      cout << "Please enter your pet's name: "; 
      cin.get(newAnimal[count].name, NAME_SIZE, '\n'); 
      cin.ignore(100, '\n'); 

      cout << "Please enter a unique ID for your pet (combinations of numbers EG: 432FD3): "; 
      cin.get(newAnimal[count].ID, ID_SIZE, '\n'); 
      cin.ignore(100, '\n'); 

      cout << "What type of breed is your pet?: "; 
      cin.get(newAnimal[count].breed, BREED_SIZE, '\n'); 
      cin.ignore(100, '\n'); 

      cout << "How old is your pet?: "; 
      cin >> newAnimal[count].age; 
      cin.ignore(100, '\n'); 

      cout << "How much does your pet weigh? (in LBS): "; 
      cin >> newAnimal[count].weight; 
      cin.ignore(100, '\n'); 

      cout << "Please describe your pet's personality!: "; 
      cin.get(newAnimal[count].desc, DESC_SIZE, '\n'); 
      cin.ignore(100, '\n'); 

      cout << "Please explain why the pet is being put up for adoption: "; 
      cin.get(newAnimal[count].reason, REASON_SIZE, '\n'); 
      cin.ignore(100, '\n'); 

      cout << "Please enter your pet's day of birth (1-31): "; 
      cin >> newAnimal[count].day; 
      cin.ignore(100, '\n'); 

      cout << "Please enter your pet's month of birth (1-12): "; 
      cin >> newAnimal[count].month; 
      cin.ignore(100, '\n'); 

      cout << "Please enter your pet's year of birth (1900-2012) : "; 
      cin >> newAnimal[count].year; 
      cin.ignore(100, '\n'); 

      cout << "Please enter the length of time your pet has spent in a shelter (in months): "; 
      cin >> newAnimal[count].length; 
      cin.ignore(100, '\n'); 
/*** WRITES the pet ID into the list of pets***/ 
      ofstream write; 
      write.open("pets.txt", ios::app); 
      write << newAnimal[count].name << '\n'; 
      write.close(); 

/*** WRITES EACH PET INFO ****/ 
//this opens the file/creates a file if it's not made yet 
//and it writes the pet's information 
     write.open(newAnimal[count].name, ios::app);  
     write << newAnimal[count].name << '\n' 
     << newAnimal[count].ID << '\n' 
     << newAnimal[count].breed << '\n' 
     << newAnimal[count].age << '\n' 
     << newAnimal[count].weight << '\n' 
     << newAnimal[count].desc << '\n' 
     << newAnimal[count].reason << '\n' 
     << newAnimal[count].day << '\n' 
     << newAnimal[count].month << '\n' 
     << newAnimal[count].year << '\n'  
     << newAnimal[count].length << '\n'; 
//this closes the file 
     write.close(); 
} 

void petAdoption::read(animal newAnimal[]) 
{ 
    ifstream read; 
//open the file apps.txt 
    read.open("pets.txt"); 
//if apps.txt doesn't exist, then print out this error 
    if(!read) 
    { 
     cout << "pets.txt doesn't exist! This is your first time!" <<endl; 
    } 
//else if it does exist, read in the names and store them back 
//into the struct member name(s) 
    else 
    { 
//while the document isn't empty 
//read in each line 
     while(!read.eof()) 
     { 
     read.getline(newAnimal[count].name, NAME_SIZE, '\n'); 
     ++count; 
     } 
     count = count-1; 
    } 
//close the file 
    read.close(); 
    for (int i = 0; i < count; ++i) 
    { 
//open the file of the name of the app 
     read.open(newAnimal[count].name); 
//if the file doesn't exist 
//then we probably deleted it 
//without removing it from the apps.txt 
//but it prints out an error 
     if(!read) 
     { 
      cout << "invalid file!" <<endl; 
     } 
//however if the file does exist, 
//read in each line and store them back 
//into the struct members 
     while(!read.eof()) 
     { 
      read.getline(newAnimal[count].name, NAME_SIZE, '\n'); 
      read.ignore(100, '\n'); 
      read.getline(newAnimal[count].ID, ID_SIZE, '\n'); 
      read.ignore(100, '\n'); 
      read.getline(newAnimal[count].breed, BREED_SIZE, '\n'); 
      read.ignore(100, '\n'); 
      read >> newAnimal[count].age; 
      read.ignore(100, '\n'); 
      read >> newAnimal[count].weight; 
      read.ignore(100, '\n'); 
      read.getline(newAnimal[count].desc, DESC_SIZE, '\n'); 
      read.ignore(100, '\n'); 
      read.getline(newAnimal[count].reason, REASON_SIZE, '\n'); 
      read.ignore(100, '\n'); 
      read >> newAnimal[count].day; 
      read >> newAnimal[count].month; 
      read >> newAnimal[count].year; 
      read >> newAnimal[count].length; 
      read.ignore(100, '\n'); 
     } 
//close the file 
     read.close(); 
    } 
} 

int main() 
{ 
    animal newAnimal[10]; 
    petAdoption adopt; 
    adopt.read(newAnimal); 
    adopt.enroll(newAnimal);  
} 
+3

避免'while(!eof())'。 – chris

+0

我必須現在就使用它。 – user1858740

+2

你爲什麼要使用它?這是越野車,並導致各地的問題。如果這是一位老師強加給他,請對它爲什麼不好並向老師提出這個問題做一些研究。 – chris

回答

0

我想我知道發生了什麼事情。當你說read.open(「pets.txt」);它應該說read.open(「pets.txt」),ios :: app,因爲它然後它snot刪除它已經存儲的數據。同時進入你的文件瀏覽器並搜索pets.txt並閱讀它,看看發生了什麼。我希望我幫助你。