C++ ofstream没有写出来自结构数组的所有输入
问题描述:
这是我第一次在大学里为C++课程,这是我的第二个项目。所以这个项目一直强调我,但是,项目几乎完成了。 我的流媒体代码有效,但它只是用上次输入的信息创建文件,而不是存储所有信息输入。此外,我知道我用来检查手机输入的位数的代码不起作用。我有一个想法做什么,但任何帮助表示赞赏。C++ ofstream没有写出来自结构数组的所有输入
#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();