这是我第一次参加大学的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();
}
}
//*********************************
答案 0 :(得分:0)
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();