-5

I have been having some trouble on my code for my final project. I have looked everwhere and I am having a hard time so I thought I would ask on here. I need to make sure that when all the names are listed in this phonebook that they will come out in alphabetical order but as of yet I am unsure how to do that. Here is the program that i currently have! Thank you!

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

using namespace std;

struct Contact {
   string name, number, notes;
};

Contact contactList[100];
int rec_num = 0;
int num_entries;

string toUpper (string S) {
   for (int i= 0; i < S.length(); i++)
      S[i] = toupper(S[i]);
   return S;
}

void ReadFile () {
   string S;
   fstream input("PhoneData.txt");
   while (!input.eof() && !input.fail()){
      input >> contactList[rec_num].name >> contactList[rec_num].number;
      getline(input, S);
      contactList[rec_num].notes = S;
      rec_num++;
   }
   cout << "Book read." << endl;
   num_entries = rec_num;
   input.close();
   return;
}

// stores phonebook for future runs of the program
void StoreFile () {
   fstream F ("PhoneData.txt");
   rec_num = 0;

   while (rec_num < num_entries){
      F << contactList[rec_num].name << " " << contactList[rec_num].number << " " << contactList[rec_num].notes  <<  " " << endl;
      rec_num++;
   }
   cout << "Phonebook stored." << endl;
   return;
}

// adds contact
void add_name(string name, string number, string notes){
   contactList[num_entries].name = name;
   contactList[num_entries].number = number;
   contactList[num_entries].notes = notes;
   num_entries++;
   return;
}

// finds contact
void retrieve_name(string name){
   for (int i = 0; i < num_entries; i++){
      if (toUpper(contactList[i].name) == toUpper(name)) {
         cout << "Phone Number: " << contactList[i].number << endl << "Notes: " << contactList[i].notes << endl;
         return;
      }
   }
   cout << "Name not found" << endl;
   return;
}

// updates contact info
void update_name(string name){
   string new_number;
   string new_notes;
   cout<<"New Phone Number"<<endl;
   cin>> new_number;
   cout<<"New Notes"<<endl;
   cin>> new_notes;
   for (int i = 0; i < num_entries; i++){
      if (toUpper(contactList[i].name) == toUpper(name)) {
         contactList[i].number = new_number;
         contactList[i].notes = new_notes;
         return;
      }
   }
}

// deletes contact
void delete_name(string name){
   int INDEX=0;
   for (int i = 0; i < num_entries; i++){
      if (toUpper(contactList[i].name) == toUpper(name)) {
         INDEX=i;
         for ( int j=INDEX; j < num_entries; j++ ){
            contactList[j].name = contactList[j+1].name;
            contactList[j].number = contactList[j+1].number;
            contactList[j].notes = contactList[j+1].notes;
         }
      }
   }
   return;
}

void listAllContacts() {
   int i = 0;
   while (i < num_entries) {
      cout << "-- " << contactList[i].name << " " << contactList[i].number << endl << "-- " << contactList[i].notes << endl << endl;
      i++;
   }
}

int main(){
   string name, number, notes;
   string FileName;
   char command;
   FileName = "PhoneData.txt";
   ReadFile ();
   cout << "Use \"e\" for enter, \"f\" for find, \"l\" for list, \"d\" for delete, \"u\" for update, \"s\" for send message, \"q\" to quit." << endl << "Command: ";
   cin >> command;
   while (command != 'q'){
      switch (command){
         case 'e': cin >> name; cout << "Enter Number: ";
                   cin >> number; cout << "Enter Notes: ";
                   cin.ignore(); getline(cin, notes);
                   add_name(name, number, notes); break;
         case 'f': cin >> name; retrieve_name(name); break;
         case 'l':
                   listAllContacts(); break;
         case 'u': cin>> name; update_name (name);break;
         case 'd' : cin>> name; delete_name (name); break;
      }
      cout << "\nCommand: "; cin >> command;
   }
   StoreFile();
   cout << "All set !";
   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Hannah Stang
  • 11
  • 1
  • 2

1 Answers1

0

Given

Contact contactList[100];
int num_entries;

you can use std::sort to sort the list of contacts. std::sort has two forms. In the first form, you can use:

std::sort(contanctList, contactList+num_entries);

if you define operator< for Contact objects.

In the second form, you can use:

std::sort(contanctList, contactList+num_entries, myCompare);

if you define myCompare to be callable object that can compare two Contact objects.

To use the first form, change Contact to:

struct Contact {
   string name, number, notes;
   bool operator<(Contact const& rhs) const
   {
      return (this->name < rhs.name);
   }
};

If you want to the comparison of names to be case insensitive, convert both names to either uppercase or lowercase and them compare them.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • My question is in fact a duplicate here is a link to a better answer ot my question! http://stackoverflow.com/questions/5569109/c-alphabetical-insertion-sort#_=_ – Hannah Stang Apr 28 '15 at 20:48
  • @HannahStang, the question is indeed a duplicate. However, I didn't see a better answer. If one of the answers feels better to you, that's good :) – R Sahu Apr 28 '15 at 20:54