Kevers
Kevers

Reputation: 23

Need help figuring out how to ask a user for what they want to remove and removing it in an array

I have a program used to manage a database of records called "client_DB". The array "client_DB" is composed of customer cell phone call records. Each customer call record contains eight fields, which are as follows: 1) a ten digit cell phone number (string, no dashes), 2) the number of relay stations used in making the call (integer), 3) the length of the call in minutes (integer), 4) the net cost of the call (double), 5) the tax rate (double), 6) the call tax (double), 7) the total cost of the call (double) and 8) string field called "discount_aval with a value of "yes" or "no". The array client_DB has a capacity (SIZE) of 20 records.

It reads from an input file first called "client_data.txt" which is composed of these values:

9546321555  0   0   yes
5612971340  5   50  no
3051234567  8   25  no
7542346622  24  17  no
3054432762  15  30  yes
9544321011  50  100 yes
8776219988  87  82  yes
9042224556  4   5   yes
7877176590  11  1   no
5617278899  20  45  no
9546321555  4   3   yes
5612971340  79  86  no
3051234567  8   25  no
7542346622  24  118 no
3054432762  115 25  yes
9544321011  43  10  yes
8776219988  265 22  yes
9042224556  2   5   yes
7877176590  89  67  no
5617278899  40  56  no

I am pretty much almost done with this but i need help on how to write a few lines of code and where they go. I need to implement a menu in which i have, but I need help on how to ask the user for which phone number they would like to delete and delete it for the REMOVE function as well as asking what phone number to search for and return its position in the SEARCH function. I also need help on setting the precision on what gets printed to the output file "weekly_client_call_info.txt" I know i need to implement this:

 cout.setf(ios::fixed);
 cout.setf(ios::showpoint);
 cout.precision(2);

but where do i place it? Also i would love to get hints on how to add column titles to what gets printed on the output file. There is a function called Process which makes calculations, how do i set the precision to those calculations? Everything else i understand, thank you! Here my code so far: (Sorry if i made any mistakes on how to properly ask a question!)

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

    //************************************************************************
    //Name:  Kevin Dunphy           Due Date: 022113
    //Instructor: Dr. Bullard               Total Points:  100 pts
    //Assignment2: client_call.cpp          UsIDFAU: kdunphy
    //:


     using namespace std;

     const int CAPACITY = 20;

     class client_db
      {
       public:
      string cellnum;
      int numofrelay;
      int call_length;
      double net_cost;
      double tax_rate;
      double call_tax;
      double total_cost;
      string discount_aval;
         };

     bool IsFull(int); //returns true if the array is full; otherwise false.
     bool IsEmpty(int count);// returns ture if the array is empty; otherwise false.

     void Add(client_db A[], int & count, client_db & db);
     void Remove(client_db A[], int *count, string name);// removes an item from the   array if it is there
     void Print_DB(client_db A[], int count);//prints to output file
     void Call_stats(client_db A[], int count);// prints all the items in the array
     int Search_DB(client_db A[], int count, string name); //if the name is in the  array, its location is returned
    //                                        //otherwise return -1;
    //
     bool IsFull(int count)
     ////Description: Determines if the array is full
     {
  return (count == CAPACITY);
       }

     bool IsEmpty(int count)
      ////Description: Determines if the array is empty
      {
return (count == 0);
       }

      void Process (client_db A[], int count)
       {

 for(int i=0; i<count; i++)
{
if (A[i].numofrelay >=1 && A[i].numofrelay<=5)
{
    A[i].tax_rate=0.01;
    A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);

}
else if (A[i].numofrelay >=6 && A[i].numofrelay<=11)
{
    A[i].tax_rate=0.03;
    A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);


}
else if (A[i].numofrelay>=12 && A[i].numofrelay<=20)
{ 
    A[i].tax_rate=0.05;
    A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].numofrelay);


}
else if (A[i].numofrelay >=21 && A[i].numofrelay<=50)
{
   A[i].tax_rate =0.08;
   A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);


}
else if (A[i].numofrelay >50)
{
    A[i].tax_rate =0.12;
    A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);


}
A[i].call_tax = ((A[i].tax_rate)/(100))*(A[i].net_cost);
A[i].total_cost = A[i].net_cost + A[i].call_tax;
}
     }

     void Print_DB(client_db A[], int count)

     //Description: Prints the items stored in A to the standard i/o device
     {

  string filename;
  cout<<"Enter output filename: "; //geting filename
  cin>>filename;

  ofstream output; //declaring an output file stream

  output.open(filename.c_str()); // c_str() converts a C++ string into a 
                              // c-style string (char array) &
                              //open binds an ofstream to a file
  for(int i=0; i<count; i++)
  {

    output<<A[i].cellnum<<"\t"
          <<A[i].numofrelay<<"\t"
          <<A[i].call_length<<"\t"
          <<A[i].net_cost<<"\t"
          <<A[i].tax_rate<<"\t"
          <<A[i].call_tax<<"\t"
          <<A[i].total_cost<<"\t"
          <<A[i].discount_aval<<endl;

}

output.close();
    }

    int Search(client_db A[], int count, string cellnum)
    ////Description: Locates cellnumbers in A's fields
    {
for(int i=0; i<count; i++)
{
    if (cellnum == A[i].cellnum)
    {
        return i;
    }
}
return -1;
     }

      void Add(client_db A[], int &count)
      ////Description: Adds key to the array
     {
if (!IsFull(count))
{
    cout<<"Enter a cellphone number, number of relay stations and the call            lenght and if a discount is available: ";
    cin>>A[count].cellnum>>A[count].numofrelay>>A[count].call_length>>A[count].discount_aval;
    count++;

}
else
{
    cout<<"The list is full\n";
}

    }

     void Add(client_db A[], int &count, client_db &db)
       ////Description: Adds key to the array
     {
if (!IsFull(count))
{
    A[count] = db; 
    count++;

}
else
{
    cout<<"The list is FULL! \n";
}

     }
      void Remove(client_db A[], int *count, string cellnum )

        ////Description: Removes the number from the array is it is there
      {
cout<<"Remove function is called and removed the cellphone number 9546321555 \t" <<endl;

int loc = Search(A,*count,cellnum);

if (IsEmpty(*count))
{
    cout<<"A is EMPTY!\n";
    return;
}
else if (loc == -1)
{
    cout<<"key not in A\n";
}
else
{
    for(int j=loc; j<(*count)-1; j++)
    {
        A[j] = A[j+1];
    }
    (*count)--;

}
        }


       void Call_stats(client_db A[],int count) // prints to screen
      {
 for(int i=0; i<count; i++)
  {
    cout<<A[i].cellnum<<"\t"
        <<A[i].numofrelay<<"\t"
        <<A[i].call_length<<"\t"
        <<A[i].discount_aval<<endl;

}
            }
          void Menu ()
      {
cout<<"The values of the filename you entered have been recognized"<<endl;
cout<<"Please enter the letter of your application of choice"<<endl;
cout<<"       "<<endl;
    cout<<"************  WELCOME TO THE MAIN MENU  ************"<<endl;
cout<<" Add an item...........................A"<<endl;
    cout<<" Remove an item........................R"<<endl;
cout<<" Search for an item....................S"<<endl;
    cout<<" Print current data....................P"<<endl;
cout<<" Print to output file..................O"<<endl;
cout<<"****************************************************"<<endl;
        }



       int main()
         {

char answer;
char answer2;
client_db CLIENT[CAPACITY]; //declaring database
int count = 0;   //initializing count

string filename;
cout<<"Hello!, this program holds clients call data records."<<endl;
cout<<"Enter input filename: "; //geting filename
cin>>filename;

ifstream input; //declaring an input file stream

input.open(filename.c_str()); // c_str() converts a C++ string into
    while(count<CAPACITY && !input.eof()) //reading until the end of the file (eof=end-of-file)
{


    input>>CLIENT[count].cellnum
    >>CLIENT[count].numofrelay
    >>CLIENT[count].call_length
    >>CLIENT[count].discount_aval;

    count++;

}

do
    {

     Menu();
     cout<<"Please enter a command letter:  "<<endl;
cin>>answer;
client_db db;

switch (answer)
    {

    case 'A' : 
cout<<"Enter a cellphone number, number of relay stations and the call lenght and  if a discount is available: "<<endl;
cin>>db.cellnum>>db.numofrelay>>db.call_length>>db.discount_aval;
Add(CLIENT, count, db);
     break;
    case 'R' : //Remove function goes here
     break;
     case 'S' : // SEARCH function goes here

      break;
     case 'P' : Call_stats(CLIENT,count);
     break;
      case 'O' :
Process(CLIENT,count); //how do i set the precision for this?
Print_DB(CLIENT,count);
     break;
}
 cout<<"Would you like to make another command?(y/n):   "<<endl;
     cin>>answer2;
     } while (answer2 == 'Y' || answer2 == 'y');
     cout<<"Goodbye"<<endl;
 return 0;






       }

Upvotes: 0

Views: 134

Answers (1)

Kayoti
Kayoti

Reputation: 49

in your output function

void Call_stats(client_db A[],int count) // prints to screen
  {
        cout.setf(ios::fixed, ios::floatfield);
        cout << setprecision(3); // precision of 3
   }

as for the search and remove method you need to get the number from the user as int for example cin>>number then loop through your list of numbers if the user int == the list int call the remove function with the apropriate parameters

Upvotes: 0

Related Questions