Hello,

I'm writing an abstract program that keeps a record of Names and Phone numbers. I've created a Record class that inherits a Name and Phone class. All the functions work great except for delete. The delete function is supposed to replace the record value with "DELETED". The program says the record is deleted but its value is not changed. Why is this happening?

Any help is appreciated. Thanks so much.

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

const string null = ""; 

class shortName  {
   string firstName;  
   string lastName;

public:  
  
  shortName();
  shortName(string, string);  
   
   void setFirst(string First)  { firstName = First;}
   void setLast(string Last)    { lastName  = Last;}
   
   string getFirst()            { return firstName;}
   string getLast()             { return lastName;}
   
   string toString();
};


shortName::shortName()
{
  firstName = null;
  lastName  = null;
}


shortName::shortName(string First, string Last)
{
  firstName = First;
  lastName  = Last;
}
 
string shortName::toString()
{
    string Form;
    
    Form = firstName + " " + lastName;
    return Form;
}
  


class shortPhone  {
  string AreaCode;
  string Prefix;
  string Number;
 
public:
  
  shortPhone();
  shortPhone(string, string, string);
  
  void setArea(string Area)    { AreaCode = Area; }
  void setPrefix(string Pref)  { Prefix = Pref; }
  void setNumber(string Num)   { Number = Num; }
  
  string getArea()             { return AreaCode; }
  string getPrefix()           { return Prefix; }
  string getNumber()           { return Number;   }
  
  string toString();
  
};

shortPhone::shortPhone()
{
   AreaCode  = null;
   Prefix    = null;
   Number    = null;
}

shortPhone::shortPhone(string Area, string Pre, string Num)
{
   AreaCode  = Area;
   Prefix    = Pre;
   Number    = Num;
}

string shortPhone::toString()
{
    string Form;
    
    Form = "(" + AreaCode +") " + Prefix + "-" + Number;
    return Form;
}
 
class Record  {
    shortName    Name;
    shortPhone   Phone;
    
public:
    Record();

	void Menu();
	int addRecord(Record*, int);
	void showRecords(Record*, int);
	void Delete(Record*, int, string);
	void Find(Record*, int, string);

    Record(shortName, shortPhone); 
    
    Record(string, string, string, string, string); 

    void setFirst(string First)   { Name.setFirst(First); }
    void setLast(string Last)     { Name.setLast(Last);   }    
    void setArea(string Area)     { Phone.setArea(Area); }
    void setPrefix(string Prefix) { Phone.setPrefix(Prefix); }
    void setNumber(string Number) { Phone.setNumber(Number); }

   string getFirst()   { return Name.getFirst(); }
   string getLast()    { return Name.getLast();  }
   string getArea()    { return Phone.getArea(); }
   string getPrefix()  { return Phone.getPrefix(); }
   string getNumber()  { return Phone.getNumber(); }
 
   string toString();
    
};


Record::Record()
{
      ;      
}

Record::Record(shortName N, shortPhone P)
{
     Name    = N;
     Phone   = P;
}                    

string Record::toString()
{
   string Form;
   
   Form = "Name:   \n" + Name.toString()    + "\n" +
          "Phone:  \n" + Phone.toString()   + "\n";
          
   return Form;
}

int main()
{
	Record List[25];
	int Size, maxSize = 25;

	string Command;
	Record obj;

	obj.Menu();

	while(true) {

		cout << "Enter Command: ";
		cin >> Command;

		if(Command == "Quit")
			break;
		else if(Command == "Help")
			obj.Menu();
		else if(Command == "Add")
			Size = obj.addRecord(List, maxSize);
		else if(Command == "Show")
			obj.showRecords(List, Size);
		else if(Command == "Find")  {
			string FindValue;

			cout << "Enter Last Name: ";
			cin >> FindValue;
			obj.Find(List, Size, FindValue);
		}
		else if(Command == "Delete")  {
			string DeleteValue;

			cout << "Enter Last Name: ";
			cin >> DeleteValue;

			obj.Delete(List, Size, DeleteValue);
	    }
		else
			cout << "Command not found. Enter Help to display Command List." <<endl;

		 cin.ignore(200, '\n');
} 
}

void Record::Menu()
{
	cout << endl;
	cout << "Command List:" << endl;
	cout << "============================================" << endl;
	cout << "Add       :Adds Records" << endl;
	cout << "tDelete   :Deletes Records" << endl;
	cout << "tFind	  :Finds Records by Last Name" << endl;
	cout << "Show      :Shows Records" << endl;
	cout << "Help      :Displays Command List" << endl;
	cout << "Quit      :Ends Program" << endl;
	cout << "============================================" << endl;
	cout << endl;
}

int Record::addRecord(Record* List, int maxSize)
{
     int K;
     string First, Last, Area, Prefix, Number;
    
    cout << "Enter Values Below, Stop To Quit" << endl;
    
     for(K = 0 ; K < maxSize ; K++) {

        cout << "First Name: ";
		cin >> First;

        if(First == "Stop")
           break;
		cout << "Last Name: ";
		cin >> Last;
		cout << "Area Code: ";
		cin >> Area;
		cout << "Prefix: ";
		cin >> Prefix;
		cout << "Number: ";
		cin >> Number;
     
        
        List[K].setFirst(First);
        List[K].setLast(Last);
		List[K].setArea(Area);
		List[K].setPrefix(Prefix);
		List[K].setNumber(Number);
       
        cin.ignore(200, '\n');
       }
      return K;
}

void Record::Delete(Record* List, int Size, string DeleteValue)
{	
	int K;

	for(K = 0 ; K < Size ; K++)
		if(List[K].getLast() == DeleteValue) {
			cout << endl;
			cout << "==============================================" << endl;
			cout << "\tTHIS RECORD HAS BEEN DELETED" << endl;
			cout << endl;
			cout << List[K].toString() << endl;
			List[K].toString() = "DELETED";
			cout << "==============================================" << endl;
			cout << endl;
			return;

		}
		else;
		cout << "Record with Last Name: " <<DeleteValue << " was Not Found!" << endl;
}

void Record::Find(Record* List, int Size, string FindValue)
{
	int K;
	int NotFoundFlag = 0;

	for(K = 0 ; K < Size ; K++)
		if(List[K].getLast() == FindValue) { 
			cout << endl;
			cout << "==============================================" << endl;
			cout << "\tRecord with Last Name: "<< List[K].getLast() << endl; 
			cout << endl;
			cout << List[K].toString() << endl;
			cout << "==============================================" << endl;
			cout << endl;
			NotFoundFlag = 1;
		}
    
	if(NotFoundFlag == 0)
		cout << "Record with Last Name: " << FindValue << " was Not Found!" << endl;
}

 void Record::showRecords(Record* List, int Size)
{
   for(int K = 0 ; K < Size ; K++)
   {
      cout << "======================" << endl;
      cout << List[K].toString() << endl;
      cout << "======================" << endl;
   }
}

Recommended Answers

All 3 Replies

1. Why semicolon after else?

else; // empty alternative?!
  cout <<  ...

2. string toString(); - the function returns string value, not a reference to anything. Now

List[K].toString() = "DELETED";

is absolutely senseless assignment to a temporary string value returned from toString() - in other words, do nothing.
3. Why capital K for loop index? It's a common practice: use all-capitals names for macros only...

if you want to assign the last name to be equal to DELETED then you would want to have

List[K].setLast("DELETED");

Thanks so much for all the help guys. I just joined this community and so far its been very welcoming and helpful.

Thanks ArkM for pointing out the else with a semicolon, I didnt see that. My teacher taught us to use K but now i understand its not good practice. I'll be sure to use i or j.

NathanOliver you fixed my program! I also played around with setting first and last as deleted but I did it this way:

List[K].setLast() ="Deleted"

which didn't work. Putting Deleted in the brace did work.

Thanks again guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.