1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
#include <iomanip>
using namespace std;
void intro();
char * question();
void getId(int * ID, int counter);
void getnames(char**Names, int counter);
void bubbleID(int * ID, char * * names, int length);
void bubbleNames(char * * names, int * ID, int length);
void displayInfo(int * ID, char ** Names,int howmany);
int main()
{
char * choice = new char [2];
char ** Names = new char * [30];
for(int x = 0; x < 30; x++)
{
Names[x] = new char [30];
}
int * ID = new int [30];
int howmany = 0;
intro ();
do
{
choice = question();
switch(*choice)
{
case 'A':
{
getId(ID, howmany);
getnames(Names, howmany);
howmany++;
break;
}
case 'I':
{
bubbleID(ID, Names, howmany);
//displayInfo(ID, Names, howmany);
break;
}
case 'N':
{
bubbleNames(Names,ID,howmany);
//displayInfo(ID, Names, howmany);
break;
}
case 'Q':
{
cout << "Have a nice day. Bye.\n";
return 0;
}
default:
cout << "Warning. Check letter entered.\n";
}
}
while(*choice !='Q');
}
void intro ()
{
cout<<"Welcome to Larry's Friendly Roster Program.\n"
<<"Choices for commands are:\n"
<<"\tA = Add: enter more data\n"
<<"\tI = ID: view the data in ID# order\n"
<<"\tN = Name: veiw thr data in Name order\n"
<<"\tQ = Quit.\n";
}
char * question()
{
char * answer = new char [2];
cout << "Enter your choice (A,I,N,Q): ";
cin >> answer;
return answer;
}
void getId(int * IDArray, int counter)
{
cout << "Enter ID: ";
cin >> IDArray[counter];
}
void getnames(char**Names, int counter)
{
cout << "Enter Name: ";
cin.getline(Names[counter],30);
}
void bubbleID(int * ID, char * * names, int length)
{
int * tempID = new int;
char * tempname = new char [128];
for(int iteration = 1; iteration < length; iteration++)
for(int index = 0; index < length - iteration; index++)
if(ID[index] > ID[index + 1])
{
*tempID = ID[index];
ID[index]=ID[index + 1];
ID[index] = *tempID;
strcpy(tempname, names[index]);
strcpy(names[index], names[index + 1]);
strcpy(names[index + 1], tempname);
}
for(int x = 0; x < length; x++)
{
cout << setw(20) << "CWID" << setw(20) << "Names" << endl;
cout << setw(20) <<ID[x] << setw(20) << names[x]<<endl;
}
}
void bubbleNames(char ** names, int * ID, int length)
{
int * tempID = new int;
char * tempname = new char [128];
for(int iteration = 1; iteration < length; iteration++)
for(int index = 0; index < length - iteration; index++)
if(strcmp( names[index],names[index + 1]))
{
strcpy(tempname, names[index]);
strcpy(names[index], names[index + 1]);
strcpy(names[index + 1], tempname);
*tempID = ID[index];
ID[index]=ID[index + 1];
ID[index] = *tempID;
}
for(int x = 0; x < length; x++)
{
cout << setw(20) << "CWID" << setw(20) << "Names" << endl;
cout << setw(20) <<ID[x] << setw(20) << names[x]<<endl;
}
}
/*void displayInfo(int * ID, char ** Names,int howmany)
{
for(int x = 0; x < howmany; x++)
{
cout << setw(20) << "CWID" << setw(20) << "Names" << endl;
cout << setw(20) <<ID[x] << setw(20) << Names[x]<<endl;
}
}
*/
|