所以你可以领养一只宠物(现在有点小车,因为它没有读入价值)
在文件中,我会看到它从 0 变成 1;这意味着它是真的。因此它应该说被采纳。
这是读取布尔值的正确方法吗?问题出在读取功能中,我的问题是
阅读>>
#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));
}