C'est la première fois que je prends un cours de C++ au collège, et c'est mon deuxième projet. Donc, ce projet m'a stressé, cependant, le projet est presque terminé. Mon code ofstream fonctionne, mais il crée uniquement le fichier avec la dernière entrée d'informations au lieu de toutes les entrées d'informations qui sont stockées. En outre, je suis conscient que le code que j'ai utilisé pour vérifier le nombre de chiffres pour l'entrée du téléphone ne fonctionnera pas. J'ai une idée de ce qu'il faut faire, mais toute aide est appréciée.C++ ofstream n'écrit pas toutes les entrées du tableau de structure
#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();
}
}
//*********************************