//I need to modify this code by changing the sort function to make it recursive
//and add a recursive binary search that prompts the user to enter a search item.
//I have added some code for the search, which I will mark with
// :mrgreen: commented out on previous line. Please help finsh with recursion. I'm lost.
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<conio.h>
usingnamespace std;
char ProgramName[] = "Project 1.cpp"; // Used to hold the program name
int Option, *oPtr = &Option;
constint ARRAY_SIZE = 10;
char fname[ARRAY_SIZE][25]={"none","none","none","none",
"none","none","none","none","none","none"};
void StartUp(void);
void WrapUp(void);
void call(void (*)(void));
void Menu(char [ARRAY_SIZE][25]);
void Add(char [ARRAY_SIZE][25]);
void Update(char [ARRAY_SIZE][25]);
void Delete(char [ARRAY_SIZE][25]);
void Sort(char [ARRAY_SIZE][25]);
void Print(char [ARRAY_SIZE][25]);
void Printarr(char [ARRAY_SIZE][25]);
void Quit(char [ARRAY_SIZE][25]);
//:mrgreen:
void Search(char [ARRAY_SIZE][25]);
int main (void)
{
StartUp();
system("cls");
Menu(fname);
return 0;
} // End of function main
void StartUp(void){
} // End of function StartUp
void call(void (*opt)(char arr[ARRAY_SIZE][25]))
{
(*opt)(fname);
}
void Menu(char arr[ARRAY_SIZE][25])
{
void (*f[6])(char arr[ARRAY_SIZE][25]) = {Add, Update, Delete, Sort, Printarr, Quit};
do
{
system("cls");
cout << "\t\t Menu"
<< "\n\t1. Add"
<< "\n\t2. Update"
<< "\n\t3. Delete"
<< "\n\t4. Sort"
<< "\n\t5. Print"
<< "\n\t6. Quit"
//:mrgreen:
<< "\n7. Search"
<< "\n\n\tChoose and Option: ";
cin >> Option;
//:mrgreen: //6-7
if(Option < 1 || Option > 7)
{
cout << "\nPlease re-enter your selection." << endl;
_getch();
}
//:mrgreen: //6-7
} while(Option <1 || Option > 7);
call(f[Option-1]);
}
void Add(char arr[ARRAY_SIZE][25])
{
int Subscript = 0;
system("cls");
cout << "\t\t Adding Name Page\n\n";
Print(arr);
cout << "\n\tWhich element do you want to add a name to (0 - 9): ";
cin >> Subscript;
if (Subscript < 0 || Subscript > 9)
{
cout << "\n\tSorry Invalid Selection Hit Enter to Return to the Main Menu\n\t";
system("pause");
system("cls");
Menu(fname);
}
else
{
if(strcmp(arr[Subscript], "none")== 0)
{
cout << "\n\tEnter a name: ";
cin >> arr[Subscript];
system("cls");
Menu(fname);
}
else
{
cout << "\n\tA name is already in this array. If you want"
<< "\n\tto update please go back to the Main Menu and "
<< "\n\tchoose the update option.\n\t";
system("pause");
system("cls");
Menu(fname);
}
}
}
void Update(char arr[ARRAY_SIZE][25])
{
int Subscript = 0;
char choice = 'b';
system("cls");
cout << "\t\t Update Name Page\n\n";
Print(fname);
cout << "\n\tWhich element do you want to Update(0 - 9): ";
cin >> Subscript;
if(Subscript < 0 || Subscript > 9)
{
cout << "\n\tSorry Invalid Selection Hit Enter to Return to the Main Menu\n\t";
system("pause");
system("cls");
Menu(fname);
return;
}
else
{
if(strcmp(arr[Subscript], "none")< 0||strcmp(arr[Subscript], "none")> 0)
{
cout << "\tA name is already entered into this array."
<< "\n\tAre you sure you want to update this name? (Y/N)";
cin >> choice;
if(choice == 'Y' || choice == 'y')
{
cout << "\n\tEnter a name: ";
cin >> arr[Subscript];
system("cls");
Menu(fname);
return;
}
else if(choice == 'N' || choice == 'n')
{
system("cls");
Menu(fname);
return;
}
else
{
cout << "\n\tSorry Invalid Selection\n\t";
system("pause");
system("cls");
Menu(fname);
return;
}
}
else
{
if(strcmp(arr[Subscript], "none")== 0)
{
cout << "\tYou chose an array that has none listed in it"
<< "\n\tplease go back to the Main Menu and "
<< "\n\tchoose the Add option.\n\t";
system("pause");
system("cls");
Menu(fname);
return;
}
}
system("cls");
Menu(fname);
return;
}
}
void Delete(char arr[ARRAY_SIZE][25])
{
int Subscript = 0;
char choice = 'b';
system("cls");
cout << "\t\t Delete Name Page\n\n";
Print(fname);
cout << "\n\tWhich element do you want to delete(0 - 9): ";
cin >> Subscript;
if(Subscript < 0 || Subscript > 9)
{
cout << "\n\tSorry Invalid Selection Hit Enter to Return to the Main Menu\n\t";
system("pause");
system("cls");
Menu(fname);
}
else
{
if(strcmp(arr[Subscript], "none")== 0)
{
cout << "\n\tThere is nothing to delete in this array."
<< "\n\tPlease go back to the Main Menu and "
<< "\n\tchoose another option.\n\t";
system("pause");
system("cls");
Menu(fname);
}
else
{
cout << "\n\tAre you sure that you want to Delete this array? (Y/N)";
cin >> choice;
if(choice == 'Y' || choice == 'y')
{
strcpy (arr[Subscript], "none") ;
}
else if(choice == 'N' || choice == 'n')
{
cout << "\n\tNo deletions will occur.\n\t";
system("pause");
system("cls");
Menu(fname);
}
else
{
cout << "\n\tSorry Invalid Selection\n\t";
system("pause");
system("cls");
Menu(fname);
}
}
}
system("cls");
Menu(fname);
}
// **************************
// The Sort function
// Here the program puts
// the data from the array into
// alphabetical order.
// ***************
void Sort(char arr[ARRAY_SIZE][25])
{
char hold[25];
for (int Pass = 1; Pass < 10; Pass++)
{
for (int j = 0; j < 10 - 1; j++)
{
if(strcmp(arr[j], arr[j + 1]) > 0)
{
strcpy(hold, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], hold);
}
}
}
system("cls");
Menu(fname);
}
void Print(char arrNames[ARRAY_SIZE][25])
{
for(int i = 0; i < 10; i++)
{
cout << "\tarrName[" << i << "] = " << arrNames[i] << endl;
}
}
void Printarr(char arrNames[ARRAY_SIZE][25])
{
system ("cls");
cout << "\t\t Print Name Page\n\n";
for(int i = 0; i < 10; i++)
{
cout << "\tarrName[" << i << "] = " << arrNames[i] << endl;
}
cout << "\n\n\t";
system("pause");
system("cls");
Menu(fname);
}
void Quit(char arr[ARRAY_SIZE][25])
{
cout << "\n\tYou have choosen to Quit the program."
<< "\n\tPlease have a great day." << endl;
WrapUp();
}
//*******************************
//// The Search function
// This function is used
// when the user chooses to
// Search for a
// particular file.
//*********************
//:mrgreen:
void Search(char arr[10][25])
{
cout << "Which element do you want to search for:(0-9) " << endl ;
cin >> "Enter choice" >> endl;
return;
}
void WrapUp(void)
{
cout << "\n\tProgram "
<< ProgramName
<< " ended successfully.\n\t";
} // End of function WrapUp.
/*
O