2012-11-28 89 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并阅读它,看看发生了什么。我希望我帮助你。