C++ Fstreams C++;在书写上有额外的空格

C++ Fstreams C++;在书写上有额外的空格,c++,C++,我弄明白了一些东西,但我在fstreams上很烂,这简直要了我的命,对于BREED+DESC,它们和name之间有一个空格,但是当我完全移除它们时,我就没有空格了,它又能工作了。有人能告诉我我做错了什么吗 #include <iostream> #include <fstream> #include <cstring> #include <cctype> using namespace std; const int NAME_SIZE = 10

我弄明白了一些东西,但我在fstreams上很烂,这简直要了我的命,对于BREED+DESC,它们和name之间有一个空格,但是当我完全移除它们时,我就没有空格了,它又能工作了。有人能告诉我我做错了什么吗

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


const int NAME_SIZE = 100;
const int BREED_SIZE = 100;
const int DESC_SIZE = 250;
const int REASON_SIZE = 250;

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

class petAdoption
{
    public:
        petAdoption();
        void enroll(animal newAnimal[], int & count);
        void read(animal newAnimal[], int & count);
        //void display(animal & newAnimal);
        //void adopt(animal & newAnimal); 
    private:


};
petAdoption::petAdoption()
{

}

int main()
{
int count = 0;
animal * newAnimal = new animal[count];
petAdoption adopt;
adopt.read(newAnimal, count);
adopt.enroll(newAnimal, count);
delete[] newAnimal;
}


void petAdoption::read(animal newAnimal[], int & count)
{
    int pets = 0;
    ifstream read;
    read.open("pets.txt");
    if(!read)
    {
        cout << "Checking... File doesn't exist!" <<endl;
    }
    else 
    {
        while(!read.eof())
        {
        read.getline(newAnimal[pets].name, NAME_SIZE, '\n');
        read.ignore(100, '\n');
        ++pets;
        }
    count = pets + 1;
    }
    read.close();

    for (int i = 0; i < pets; ++i){
        read.open(newAnimal[i].name);
            if( !read)
            {
                cout << "Checking... File doesn't exist!" <<endl;
            }
        else{
            while(!read.eof())
            {
                read.getline(newAnimal[i].name, NAME_SIZE, '\n');
                read.getline(newAnimal[i].breed, BREED_SIZE, '\n');
                read.getline(newAnimal[i].desc, DESC_SIZE, '\n');
                read.getline(newAnimal[i].reason, REASON_SIZE, '\n');
                read.ignore(100, '\n');
                read >> newAnimal[i].age;
                read >> newAnimal[i].weight;
                read >> newAnimal[i].day;
                read >> newAnimal[i].month;
                read >> newAnimal[i].year;
                read >> newAnimal[i].length;
                read.ignore(100, '\n');
            }
        }
        read.close();
    }
}


/*
ENROLL FUNCTION
*/

void petAdoption::enroll(animal newAnimal[], int & count)
{
//making a write variable
    ofstream write;

//stores the name into the struct member
    cout << "Please enter a name: ";
    cin.get(newAnimal[count].name, NAME_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter a breed: ";
    cin.get(newAnimal[count].breed, BREED_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter a desc: ";
    cin.get(newAnimal[count].desc, DESC_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter a reason (EG: eats people): ";
    cin.get(newAnimal[count].reason, REASON_SIZE, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter an age (EG: 5): ";
    cin >> newAnimal[count].age;
    cin.ignore(100, '\n');
    cout << "Please enter a weight (in LBS): ";
    cin >> newAnimal[count].weight;
    cin.ignore(100, '\n');
    cout << "Please enter a day (EG: 2): ";
    cin >> newAnimal[count].day;
    cout << "Please enter a month (EG: 11): ";
    cin >> newAnimal[count].month;
    cout << "Please enter a year (EG: 2002): ";
    cin >> newAnimal[count].year;
    cout << "How long has the pet been in a shelter? (in months): ";
    cin >> newAnimal[count].length;


//error
    if(!write)
    {
        cout << "Invalid File" << endl;
    }
    else{
        write.open(newAnimal[count].name, ios::app);    
        write << newAnimal[count].name << '\n'
        << newAnimal[count].breed << '\n'
        << newAnimal[count].desc << '\n'
        << newAnimal[count].reason << '\n'
        << newAnimal[count].age << '\n'
        << newAnimal[count].weight << '\n'
        << newAnimal[count].day << '\n'
        << newAnimal[count].month << '\n'
        << newAnimal[count].year << '\n'
        << newAnimal[count].length << '\n';
        write.close();
    }
    if(!write) 
    {
        cout << "Invalid File";
    }
    else {
        write.open("pets.txt", ios::app);
        write << newAnimal[count].name << '\n';
        write.close();  
    }               
}
#包括
#包括
#包括
#包括
使用名称空间std;
常量int NAME_SIZE=100;
const int bread_SIZE=100;
const int DESC_SIZE=250;
const int REASON_SIZE=250;
//创建结构
结构动物
{
//成员
字符名称[名称大小];
char品种[品种大小];
字符描述[描述大小];
字符原因[原因大小];
浮动年龄;
浮重;
国际日;
整月;
国际年;
浮子长度;
};
类掺杂
{
公众:
petAdoption();
无效登记(动物新动物[],整数和计数);
无效读取(动物新动物[],整数和计数);
//无效显示(动物和新动物);
//无效领养(动物和新动物);
私人:
};
petAdoption::petAdoption()
{
}
int main()
{
整数计数=0;
动物*新动物=新动物[计数];
petAdoption采用;
领养。阅读(新动物,计数);
领养、登记(新动物、计数);
删除[]新动物;
}
void petAdoption::read(animal newAnimal[],int&count)
{
int=0;
ifstream读取;
read.open(“pets.txt”);
如果(!读取)
{
新动物[i].天;
阅读>>新动物[i].月;
阅读>>新动物[i].年;
阅读>>新动物[i].长度;
读。忽略(100,“\n”);
}
}
read.close();
}
}
/*
注册函数
*/
void petAdoption::登记(动物新动物[],整数和计数)
{
//生成写变量
流式写入;
//将名称存储到结构成员中
cout newAnimal[计数]。月份;
cout>newAnimal[count]。年份;
cout>newAnimal[count]。长度;
//错误
如果(!写入)
{
cout正如人们提到的(已经回答了这个问题的早期版本),你不应该使用
eof()
来控制你的循环。此外,你应该测试你在[尝试]阅读后是否成功地收到了你的输入


如果一行由于使用<代码> GET()/<代码>而不是<代码> GETLIN()/代码>:前者保留了停止字符,而后者不使用,则应考虑使用“<代码> STD::String < /代码>和<代码> STD::GETLIN()。

:它可以读取任意长度的字符串。

为什么你使用
char
数组而不是
std::string
?*打哈欠*
而(!read.eof())
是错误的。是什么资源在教人们这一点?你能清楚地提到,问题是什么吗?我想找到在(!file.eof())的同时教授
的讲师
然后用石头砸死他们。除此之外,你能发布一些样本数据吗?请不要太大。@cppcoder问题是当我放入所有数据时,当它保存文件时,它的名称(新行)(新行)繁殖等。我必须使用字符数组。当我使用getline时,它也有同样的问题。所以我想我应该尝试get。不,你不必使用字符数组。两件事:1.由于你一直附加到一个文件,请确保该文件最初为空,并且你没有试图调试一个问题,因为你第一次编写它。2.创建一个小示例,显示pr问题。也就是说,复制代码并删除所有内容,直到只有显示问题的代码出现为止。我想在您的情况下,它不应该超过20行。…和
get()
getline()
我所说的方式显然有所不同。我认为我的问题在于注册函数,但老实说,我已经尝试修复它3个小时了。我吸xD。过了一段时间,它似乎只是在linux中冻结了我的cmd,O.oth定位问题的首要方法是隔离它们!您的源代码很想让我知道问题在哪里。如果您你可以把它简化为几行,创建一个不符合原始目标但也有问题的程序,解决问题往往要简单得多。顺便说一句,我在你的图片上看到一个错误“503服务不可用”。