Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Put together a program that keeps track of monetary contributions to a charitabl

ID: 3589021 • Letter: P

Question

Put together a program that keeps track of monetary contributions to a charitable fund.

1.  In the start of the program the user will be asked about the total number of the contributors who are going to contribute to the charitable fund.

2.  Once the number of the contributors is known he would create a dynamically allocated array of structure.

3.  The structure will consist of the 4 members. First member will store the name of the contributor. The second member will store the amount of contribution (this should be able to store the ecimal number), the third member should store the telephone number of the contributor in 10 digit (use character array or string for this. This should be stored like 2344551345), and the fourth member will store the class of the contributor (‘Platinum’, ‘Diamond’, ‘Gold’, ‘Silver’).

4.  Loop through the total number of contributor and accept the input from the user about name, amount and the telephone number of each contributor and store them in the array declared in the step 2. The class would be determined based on the amount of contribution. If the contribution is more than 10000 it is Platinum, if it is from 5001 to 10000 it is Diamond, if it is from 1001 to 5000 it is Gold and if it is from 500 to 1000 it is Silver.

5.  While accepting the input from the user, program must check that the minimum contribution is $ 500 and the maximum contribution is $20000. If the contribution entered by the user is not in this range, program should create an error message and ask the user to enter the number again for the contribution.

6.  The input for the telephone number must be of 10 digits. If it is not an error message must be created and user should be asked to reenter the telephone number. Also make sure there is no alphabet in the telephone number. It consists of numbers only.

7.  After reading all the data, the program should display the names, amounts donated by the contributors. First the Platinum ones, then the Diamond ones, then the Gold ones and finally the Silver ones.

8.  The output created in the step 7, should be written to a text file called ‘Charity.txt’

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>


using namespace std;

struct Contributor{
  
      string name;
      double amount;
      string phone;
      string Class;

};


int main(){

     int n;
     ofstream fout;
     cout << "Enter number of contributors :";
     cin >> n;

     Contributor *data = new Contributor[n];

     for (int i = 0; i<n; i++){
          cout << "Enter name :";
          cin >> data[i].name;
          while(true){
             cout << "Enter amount :";
             cin >> data[i].amount;
             if (data[i].amount < 500 && data[i].amount > 20000)
                cout << "Incorrect data. Valid range 500-20000 ";
             else
                break;
          }
          while(1){
              cout << "Enter phone :";
              cin >> data[i].phone;
              if (data[i].phone.size() != 10)
                 cout << "Enter only 10 digits ";
              else{
                 int found = 0;
                 for (int j = 0; j<10; j++){
                     if (data[i].phone[j] < '0' || data[i].phone[j] > '9'){
                        found = 1;
                        break;
                     }
                 }
                 if (found == 0)
                    break;
                 else {
                     cout << "Phone only contains digits ";
                 }
              }
          }
          if (data[i].amount > 10000)
             data[i].Class = "Platinum";
          if (data[i].amount >= 5001 && data[i].amount <=10000)
             data[i].Class = "Diamond";
          if (data[i].amount >= 1001 && data[i].amount <= 5000)
             data[i].Class = "Gold";
          if (data[i].amount >= 500 && data[i].amount <= 1000)
             data[i].Class = "Silver";
     }
     fout.open("Charity.txt");
    
     for (int i= 0; i<4; i++){
        for (int j = 0; j<n; j++){
           
            if (i==0){
               
               if (data[j].Class == "Platinum"){
                  cout << data[j].name << " " << data[j].amount << endl;
                  fout << data[j].name << " " << data[j].amount << endl;
               }
            }
            if (i==1){
               if (data[j].Class == "Diamond"){
                  cout << data[j].name << " " << data[j].amount << endl;
                  fout << data[j].name << " " << data[j].amount << endl;
               }
            }
            if (i==2){
               if (data[j].Class == "Gold"){
                  cout << data[j].name << " " << data[j].amount << endl;
                  fout << data[j].name << " " << data[j].amount << endl;
               }
            }
            if (i==3){
               if (data[j].Class == "Silver"){
                  cout << data[j].name << " " << data[j].amount << endl;
                  fout << data[j].name << " " << data[j].amount << endl;
               }
            }

        }
     }
    
     fout.close();
     return 0;
}