所以你可以收养一只宠物(现在有种马车,因为它没有读入价值)
在文件中我会看到它从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 :(得分:0)
似乎问题在于你的逻辑,我已经在你的代码中修复了一些东西并且它正在工作看看: 我修的是: - 注册(动物宠物[],int&amp; count) - getNum(int&amp; count) - 阅读(动物宠物[],int&amp; count) - 显示(动物宠物[],int&amp; 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();
}