This is my first time taking a class for c++ in college, and this is my second project. So this project has been stressing me out, however, the project is almost done. My ofstream code works, but it is only creating the file with the last input of information instead of all inputs of information that are stored. Also, I am aware that the code I used to check the number of digits for the phone input will not work. I have an idea of what to do, but any help is appreciated.
#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();
}
}
//*********************************