C++ ofstream 没有写入结构数组的所有输入

C++ ofstream isn't writing all of the inputs from the structure array

这是我在大学里第一次参加 class 的 C++ 课程,这是我的第二个项目。所以这个项目一直让我压力很大,然而,这个项目快完成了。 我的 ofstream 代码有效,但它只使用最后输入的信息而不是存储的所有信息输入创建文件。另外,我知道我用来检查 phone 输入位数的代码将不起作用。我知道该怎么做,但任何帮助表示赞赏。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;



struct Contributor
{
string firstName;
string lastName;
double amount;
string number;
string class_con;
};
//Prototype************************
void input();
void display();
//*********************************

//Global Variable******************
Contributor*contributors = NULL;
int count;
//*********************************

//Program will run here************
int main()
{
    input();
    display();
}
//*********************************

//Functions to be prototype********
void input()
{

cout << "Enter the number of Contributors" << endl;
cin >> ::count;

contributors = new Contributor[::count];

for (int i = 0; i < ::count; i++)
{
    bool amount_correct = false; bool phone_correct = false;
    //Name Member
    cout << "First Name and Last Name of contributor" << endl;
    cin >> contributors[i].firstName >> contributors[i].lastName;
    //Amount Member
    double amount;
    cout << "Enter the amount[amount>500 && amount<20000]" << endl;
    cin >> amount;
    while (!amount_correct)
    {
        if (amount >= 500 && amount <= 20000)
        {
            contributors[i].amount = amount;
            amount_correct = true;
        }
        else
        {
            cout << "Invalid amount!! Please re-enter a value that is greater than 500, and less than 20000: " << endl;
            cin >> amount;
        }
    }
    {//phone member
        string phone;
        cout << "Enter phone number: ";
        cin >> phone;
        while (!phone_correct)
        {
            bool flag = false;
            for (int j = 0; j < (phone.length() - 1); j++)
            {
                if (!((int)phone[j] < 10))
                {
                    flag = true;
                }
            }
            if (flag)
            {
                contributors[i].number = phone;
                phone_correct = true;
            }
            else
            {
                cout << "Invalid phone number!! Please re-enter a 10 digit number: ";
                cin >> phone;
            }
        }
    }//End of phone member

    {//Class of contributor
    //Platinum Class
        if (contributors[i].amount >= 10000)
        {
            contributors[i].class_con = "Platinum";
        }
        //Diamond Class
        else if (contributors[i].amount >= 5000 && contributors[i].amount <= 10000)
        {
            contributors[i].class_con = "Diamond";
        }
        else if (contributors[i].amount >= 1000 && contributors[i].amount <= 5000)
        {
            contributors[i].class_con = "Gold";
        }
        else
        {
            contributors[i].class_con = "Silver";
        }
        cout << "\n";
    }//End of Class member
}
}

void display()
{
cout << "-------------------------" << endl;
cout << "First Name, Last Name--Amount----Class----Telephone" << endl;
cout << "-------------------------" << endl;

for (int i = 0; i < ::count; i++)
{
    cout << "\nName: " << contributors[i].firstName << " " << contributors[i].lastName << endl;
    cout << "\nAmount Contributed: " << contributors[i].amount << endl;
    cout << "\Class of Contributor: " << contributors[i].class_con << endl;
    cout << "\nPhone: " << contributors[i].number << endl;
    cout << "\n";
}
for (int i = 0; i < ::count; i++)
{
    ofstream file;
    file.open("charity.txt");
    file <<"Name :"<< contributors[i].firstName << " " << contributors[i].lastName << endl;
    file<<"Amount "<< contributors[i].amount << endl;
    file<<"Class: "<< contributors[i].class_con << endl;
    file<<"Telephone Number: "<< contributors[i].number << endl;
    file << endl;
    file.close();
}
}
//*********************************
for(int i = 0; i < ::count; i++)
{
    ofstream file;
    file.open("charity.txt");
    file << ...
    file.close();
}

每次打开文件时都会覆盖旧数据。您应该将文件打开为 file.open("charity.txt", std::ios::app) 以将数据追加到旧数据之后。或者只打开文件一次如下:

ofstream file;
file.open("charity.txt");
for(int i = 0; i < ::count; i++)
{
    file << ...
}
file.close();