Last active
December 16, 2015 18:49
-
-
Save danamuise/5480352 to your computer and use it in GitHub Desktop.
Comp130 Term project 4/29/2013
Function Definitions.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************* DRIVER ******************************************************** | |
File: EndagererSpecies_Functions.cpp | |
Name: Dana Muise | |
Date: 4/29/2013 | |
Purpose: Functions for Endagered Species application | |
*/ | |
#include <iostream> | |
#include <ctype.h> | |
#include<string> | |
#include <fstream> | |
#include <iomanip> | |
#include "conio.h" | |
#include <limits> | |
#include <sstream> | |
#include "EndageredSpecies_H.h" | |
using namespace std; | |
namespace danaMuise | |
{ | |
void endangeredSpecies() | |
{ | |
splashPage(); | |
//Instantiate a structure object: animals | |
Endangered animals[200]; | |
int stringLength=0; | |
//initialize animals | |
for (int i=0; i<200; i++) | |
{ | |
animals[i].name="null"; | |
animals[i].population=0; | |
animals[i].species='X'; | |
} | |
readFile(animals, stringLength); | |
mainMenu(animals, stringLength); | |
_getch(); | |
} | |
int mainMenu (Endangered animals[], int &stringLength) | |
{ | |
cout << "\n\t********** Main Menu **********"<<endl; | |
cout << "\n\t[D] Display complete list"<<endl; | |
cout << "\n\t[M] Display most endangered of a particular species"<<endl; | |
cout << "\n\t[A] Add an animal to the list"<<endl; | |
cout << "\n\t[X] Delete an animal from the list"<<endl; | |
cout << "\n\t[E] Edit an animal in the list"<<endl; | |
cout << "\n\t[K] Consolidate duplicate entries in the list"<<endl; | |
cout << "\n\t[C] Correct capitalization inconsistencies in the names"<<endl; | |
cout << "\n\t[Q] Quit"<<endl; | |
cout << "\n\n\n\tPlease make a selection from the menu above:" << endl; | |
cout << "\n\n\t"; | |
char menuChoice=' '; | |
int choiceValid=0; | |
while (choiceValid==0) | |
{ | |
cin >> menuChoice; | |
if (islower(menuChoice)) menuChoice=toupper(menuChoice); | |
switch (menuChoice) | |
{ | |
case ('D'): | |
displayList(animals, stringLength); | |
choiceValid++; | |
break; | |
case ('M'): | |
displayMostEndangered(animals, stringLength); | |
choiceValid++; | |
break; | |
case ('A'): | |
addEntry(animals, stringLength); | |
choiceValid++; | |
break; | |
case ('X'): | |
deleteEntry(animals, stringLength); | |
break; | |
case ('E'): | |
editEntry(animals, stringLength); | |
choiceValid++; | |
break; | |
case ('C'): | |
caseFix (animals, stringLength); | |
choiceValid++; | |
break; | |
case ('K'): | |
consolidate (animals, stringLength); | |
choiceValid++; | |
break; | |
case ('Q'): | |
saveOut (animals, stringLength); | |
choiceValid++; | |
return 0; | |
default: | |
cout << "\n\tInvalid selection! Please try again. "<<endl; | |
cout << "\n\t"; | |
break; | |
} | |
} | |
} | |
int readFile(Endangered animals[], int &stringLength) | |
{ | |
string tempString1=" "; | |
string tempString2=" "; | |
string tempString3=" "; | |
int i=0; | |
ifstream myFile; | |
myFile.open("animals.txt"); | |
if (myFile.is_open()) | |
{ | |
while(!myFile.eof()) | |
{ | |
//read the string until delimiter # | |
getline(myFile, tempString1, '#'); | |
animals[i].name=tempString1; | |
//read the string until delimiter # | |
getline(myFile, tempString2, '#'); | |
animals[i].population=stoi(tempString2); | |
//read the string until delimiter /n | |
getline(myFile, tempString3); | |
animals[i].species=tempString3.at(0); | |
i++; | |
stringLength++; | |
}//end while | |
myFile.close(); | |
}//end if | |
else | |
{ | |
cout << "File could not be opened." << endl; | |
return 1; | |
} | |
}//end readFile() | |
void displayList(Endangered animals[], int stringLength) | |
{ | |
cout <<"\n\t"<<setw(3)<<"Item#: "<<setw(23)<< "Name"<<setw(17)<< "Population" << setw(11) <<"Species"<<endl; | |
cout <<"\t=================================================================="<< endl; | |
for (int i=0; i<stringLength; i++) | |
{ | |
cout << "\t" <<setw(3)<< i+1 << setw(28) << animals[i].name <<setw(10)<<animals[i].population<<setw(12)<<animals[i].species<<endl; | |
} | |
miniMenu(animals, stringLength); | |
} | |
void displayMostEndangered(Endangered animals[], int stringLength) | |
{ | |
int counter=0; | |
char select=' '; | |
string amountString=" "; | |
int amount=0; | |
int amountLength=0; | |
Endangered tempArray[200]; | |
//initialize birds | |
for (int i=0; i<200; i++) | |
{ | |
tempArray[i].name="null"; | |
tempArray[i].population=0; | |
tempArray[i].species='X'; | |
} | |
int valid=0; | |
int validAmount=0; | |
//Test if input is valid | |
while(valid==0) | |
{ | |
cout << "\n\tWhat species would you like to display? [M=Mammal, B=Bird, F=Fish]: "; | |
cin >> select; | |
if (islower(select)) select=toupper(select); | |
switch (select) | |
{ | |
case ('M'): | |
cout << "\n\tHow many most endangered mamals would you like to see? "; | |
amount=getAndCheckInt(); | |
cout << "\n\n\t******** TOP "<< amount << " MOST ENDANGERED MAMMALS ********"<< endl; | |
for (int i=0; i<=stringLength; i++) | |
{ | |
//Collect all the mammals into a temporary array | |
if (animals[i].species=='M') | |
{ | |
tempArray[counter].name=animals[i].name; | |
tempArray[counter].population=animals[i].population; | |
tempArray[counter].species=animals[i].species; | |
counter++; | |
} | |
} | |
//Sort the mamals from lowest population to highest | |
bubbleSort (tempArray, counter); | |
//Draw the masthead | |
cout <<"\n"<<setw(3)<<"\tRank "<<setw(23)<< "Name"<<setw(17)<< "Population" <<endl; | |
cout <<"\t=================================================="<< endl; | |
//Display the amount requested | |
if (amount>counter) amount=counter; | |
for (int i=0; i<amount; i++) | |
{ | |
cout <<"\t" << setw(3)<< i+1 << setw(28) << tempArray[i].name <<setw(10)<<tempArray[i].population<<endl; | |
} | |
cout << endl; | |
cout << endl; | |
valid=1; | |
break; | |
case ('B'): | |
cout << "\n\tHow many most endangered birds would you like to see? "; | |
amount=getAndCheckInt(); | |
cout << "\n\n\t******** TOP "<< amount << " MOST ENDANGERED BIRDS ********"<< endl; | |
for (int i=0; i<=stringLength; i++) | |
{ | |
//Collect all the Birds into a temporary array | |
if (animals[i].species=='B') | |
{ | |
tempArray[counter].name=animals[i].name; | |
tempArray[counter].population=animals[i].population; | |
tempArray[counter].species=animals[i].species; | |
counter++; | |
} | |
} | |
//Sort the birds from lowest population to highest | |
bubbleSort (tempArray, counter); | |
//Draw the masthead | |
cout <<"\n"<<setw(3)<<"\tRank "<<setw(23)<< "Name"<<setw(17)<< "Population" <<endl; | |
cout <<"\t=================================================="<< endl; | |
//Display the amount requested | |
if (amount>counter) amount=counter; | |
for (int i=0; i<amount; i++) | |
{ | |
cout <<"\t" << setw(3)<< i+1 << setw(28) << tempArray[i].name <<setw(10)<<tempArray[i].population<<endl; | |
} | |
cout << endl; | |
cout << endl; | |
valid=1; | |
break; | |
case ('F'): | |
cout << "\n\tHow many most endangered fish would you like to see? "; | |
amount=getAndCheckInt(); | |
cout << "\n\n\t******** TOP "<< amount << " MOST ENDANGERED FISH ********"<< endl; | |
for (int i=0; i<=stringLength; i++) | |
{ | |
//Collect all the fish into a temporary array | |
if (animals[i].species=='F') | |
{ | |
tempArray[counter].name=animals[i].name; | |
tempArray[counter].population=animals[i].population; | |
tempArray[counter].species=animals[i].species; | |
counter++; | |
} | |
} | |
//Sort the fish from lowest population to highest | |
bubbleSort (tempArray, counter); | |
//Draw the masthead | |
cout <<"\n"<<setw(3)<<"\tRank "<<setw(23)<< "Name"<<setw(17)<< "Population" <<endl; | |
cout <<"\t=================================================="<< endl; | |
//Display the amount requested | |
if (amount>counter) amount=counter; | |
for (int i=0; i<amount; i++) | |
{ | |
cout <<"\t" << setw(3)<< i+1 << setw(28) << tempArray[i].name <<setw(10)<<tempArray[i].population<<endl; | |
} | |
cout << endl; | |
cout << endl; | |
valid=1; | |
break; | |
default: | |
cout << "\n\tInvalid selection. Please try again." << endl; | |
break; | |
}//end switch case | |
}//end while valid | |
miniMenu(animals, stringLength); | |
}//end displayMostEndangered() | |
void addEntry(Endangered animals[], int &stringLength) | |
{ | |
int x=0; | |
int newIndex=0; | |
bool found=false; | |
//Find the next empty index on the list | |
while (found==false) | |
{ newIndex++; | |
if (animals[newIndex].name=="null") found=true; | |
}//end while (found) | |
string tempString="null"; | |
bool again=true; | |
while (again) | |
{ | |
cin.ignore(); | |
bool loop=true; | |
while (loop) | |
{ | |
cout << "\n\tEnter a new animal name: "; | |
getline(cin, animals[newIndex].name); | |
cin.clear(); | |
if (animals[newIndex].name=="") cout << "\n\tError: You must enter a name!" << endl; | |
else loop=false; | |
} | |
cout << "\n\tEnter new animal's current population: "; | |
x=getAndCheckInt(); | |
animals[newIndex].population=x; | |
bool switchLoop=true; | |
char menuChoice=' '; | |
while (switchLoop) | |
{ | |
cout << "\n\tEnter animal species type ( M=mammal, B=bird, F=fish ): "; | |
cin >> menuChoice; | |
if (islower(menuChoice)) menuChoice=toupper(menuChoice); | |
//Switch case to validate input | |
switch (menuChoice) | |
{ | |
case ('M'): | |
animals[newIndex].species='M'; | |
switchLoop=false; | |
break; | |
case ('B'): | |
animals[newIndex].species='B'; | |
switchLoop=false; | |
break; | |
case ('F'): | |
animals[newIndex].species='F'; | |
switchLoop=false; | |
break; | |
default: | |
cout << "\n\tInvalid selection. Please try again." << endl; | |
break; | |
}//end switch | |
}//end while(switchLoop) | |
stringLength++; | |
//Display new animal | |
cout << "\n\tNew entry #" << newIndex << ": " << endl; | |
cout << "\tName: " << animals[newIndex].name << endl; | |
cout << "\tpopulation: " << animals[newIndex].population << endl; | |
cout << "\tspecies: " << animals[newIndex].species << endl; | |
//Again? | |
cout << "\n\tAdd another animal? ( y / n ) "; | |
again=againYN(); | |
newIndex++; | |
}//end while | |
cout << "\n\tNumber of items on the list: " << stringLength<<endl; | |
miniMenu(animals, stringLength); | |
}//end function | |
void bubbleSort (Endangered tempArray[], int counter) | |
{ | |
for(int x=0; x<=counter; x++) | |
{ | |
for(int y=0; y<counter-1; y++) | |
{ | |
if(tempArray[y].population>tempArray[y+1].population) | |
{ | |
string temp1 = tempArray[y+1].name; | |
tempArray[y+1].name = tempArray[y].name; | |
tempArray[y].name = temp1; | |
int temp2 = tempArray[y+1].population; | |
tempArray[y+1].population = tempArray[y].population; | |
tempArray[y].population = temp2; | |
char temp3 = tempArray[y+1].species; | |
tempArray[y+1].species = tempArray[y].species; | |
tempArray[y].species = temp2; | |
} | |
} | |
} | |
} | |
void caseFix (Endangered animals[], int stringLength) | |
{ | |
string tempString=" "; | |
for(int i=0; i<stringLength; i++) | |
{ | |
tempString=animals[i].name; | |
for(int x=0; x<tempString.length(); x++) | |
{ | |
if (isupper(tempString[x])) tempString[x]=tolower(tempString[x]); | |
}//end for x | |
animals[i].name=tempString; | |
// first char uppercase | |
for(int y=0; y<=stringLength; y++) | |
{ | |
tempString=animals[y].name; | |
if (islower(tempString[0])) tempString[0]=toupper(tempString[0]); | |
animals[y].name=tempString; | |
}//end for y | |
}//end for i | |
cout << "\n\tAll animal names now begin with an uppercase letter" << endl; | |
miniMenu(animals, stringLength); | |
} | |
void consolidate (Endangered animals[], int &stringLength) | |
{ | |
for(int i=0; i<stringLength; i++) | |
{ | |
int counter=i+1; | |
while (counter<=stringLength) | |
{ | |
//Compare one index to the next index | |
if(animals[i].name==animals[counter].name) | |
{ | |
animals[i].population = animals[i].population + animals[counter].population; | |
stringLength--; | |
//Delete duplicate index | |
for (int x=counter; x<=stringLength; x++) | |
{ | |
animals[x].name = animals[x+1].name; | |
animals[x].population = animals[x+1].population; | |
animals[x].species = animals[x+1].species; | |
} | |
} | |
if (animals[i].name != animals[i+1].name) counter++; | |
} | |
} | |
cout << "\n\tAll animal names now consolidated" << endl; | |
cout << "\n" << endl; | |
cout << "\n\tNumber of items on the list: " << stringLength<<endl; | |
miniMenu(animals, stringLength); | |
} | |
void editEntry(Endangered animals[], int stringLength) | |
{ | |
int edit=0; | |
cout << "\n\tEnter the item number you wish to edit: "; | |
cin >> edit; | |
edit--; | |
cout << "\n\tItem #: "<< edit+1 << "\n\tName: " << animals[edit].name<<"\n\tPopulation: " <<animals[edit].population<<"\n\tSpecies: " <<animals[edit].species<<endl; | |
char answer=' '; | |
bool switchLoop=true; | |
while (switchLoop) | |
{ | |
cout << "\n\n\tWhat part would you like to edit? \n\t[N]=name, [P]=population, [S]=Species "; | |
cin >> answer; | |
if (islower(answer)) answer=toupper(answer); | |
switch (answer) | |
{ | |
case 'N': | |
cout << "\n\tEdit Name: "; | |
//Flush cin buffer | |
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | |
//Get new name | |
getline(cin, animals[edit].name); | |
cin.clear(); | |
cout << "\n\tContinue editing this item? ( y/n )"; | |
switchLoop=againYN(); | |
break; | |
case 'P': | |
cout <<"\n\tEdit Population: "; | |
animals[edit].population=getAndCheckInt(); | |
cout << "\n\tContinue editing this item? ( y/n )"; | |
switchLoop=againYN(); | |
break; | |
case 'S': | |
editSpecies(animals, edit); | |
cout << "\n\tContinue editing this item? ( y/n )"; | |
switchLoop=againYN(); | |
break; | |
default: | |
cout << "\n\tInvalid selection! Please try again. "; | |
break; | |
}//end switch case | |
}//end While | |
cout << "\n\tItem #: "<< edit+1 << " Name: " << animals[edit].name<<" Population: " <<animals[edit].population<<" Species: " <<animals[edit].species<<endl; | |
cout << "\n" << endl; | |
miniMenu(animals, stringLength); | |
}//end editEntry() | |
int saveOut (Endangered animals[], int stringLength) | |
{ | |
char answer=' '; | |
bool loop=true; | |
while (loop) | |
{ | |
cout << "\n\tWould you like to save the changes you made to this list? (y / n) "; | |
cin >> answer; | |
if (islower(answer)) answer=toupper(answer); | |
if (answer=='Y') | |
{ | |
ofstream myFile; | |
myFile.open("animals.txt"); | |
if (myFile.is_open()) | |
{ | |
for (int i=0; i<stringLength; i++) | |
{ | |
myFile << animals[i].name <<'#'<<animals[i].population<<'#'<<animals[i].species<<endl; | |
//cout << animals[i].name <<'#'<<animals[i].population<<'#'<<animals[i].species<<endl; | |
}//end for | |
myFile.close(); | |
cout << "\n\tChanges saved." << endl; | |
loop=false; | |
}//end if | |
else | |
{ | |
cout << "\n\tFile could not be opened." << endl; | |
return 1; | |
}//end else | |
}//end if | |
if (answer=='N')loop=false; | |
else cout << "\n\tInvalid answer, try again: "<< endl; | |
}//end loop | |
cout << "\n\tPress any key to quit" << endl; | |
_getch; | |
}//end saveOut() | |
void deleteEntry(Endangered animals[], int &stringLength) | |
{ | |
int x=0; | |
int valid=0; | |
while(valid==0) | |
{ | |
cout << "\n\tEnter the number you would like to delete from the list: "; | |
cin >> x; | |
if (x<1) cout << "\n\tInvalid! Enter positive integers only. "<<endl; | |
if (!isdigit(x)) cout << "\n\tInvalid! Enter integers only. "<<endl; | |
if (x>stringLength) cout << "\n\tInvalid! Please choose an item between 1 and " << stringLength <<endl; | |
else valid=1; | |
} | |
cout << "\n\tItem #: "<< x << " Name: " << animals[x-1].name<<" Population: " <<animals[x-1].population<<" Species: " <<animals[x-1].species<<endl; | |
cout << "\n\tAre you sure you want to delete? (y/n)"; | |
char answer=' '; | |
valid=0; | |
while(valid==0) | |
{ | |
cin >> answer; | |
if (islower(answer)) answer=toupper(answer); | |
if(answer=='Y') | |
{ | |
for (int i=x-1; i<stringLength; i++) | |
{ | |
animals[i].name=animals[i+1].name; | |
animals[i].population=animals[i+1].population; | |
animals[i].species=animals[i+1].species; | |
} | |
stringLength--; | |
valid=1; | |
} | |
if (answer=='N') valid=1; | |
if (answer!='Y' && answer!='N') cout << "\n\tInvalid answer, Are you sure you want to delete? (y/n): "; | |
}//end while | |
cout << "\n" << endl; | |
cout << "\n\tNumber of items on the list: " << stringLength<<endl; | |
miniMenu(animals, stringLength); | |
} | |
void miniMenu(Endangered animals[], int stringLength) | |
{ | |
int valid=0; | |
while(valid==0) | |
{ | |
cout << "\n\tPress [M] for main menu: "<<endl; | |
cout << "\n\t"; | |
char answer=' '; | |
cin >> answer; | |
if (islower(answer)) answer=toupper(answer); | |
if (answer=='M') valid=1; | |
else cout << "\n\tInvalid slection, try again: "<< endl; | |
}//end while | |
mainMenu(animals, stringLength); | |
} | |
int getAndCheckInt() | |
{ | |
string str=""; | |
int x=0; | |
bool loop=true; | |
while (loop) | |
{ | |
// cin.ignore(numeric_limits<streamsize>::max(), '\n'); | |
cin.clear(); | |
getline (cin,str); | |
int stringLength=str.length(); | |
for (int i=0; i<stringLength; i++) | |
{ | |
if (str[i]>=48 && str[i]<=57) | |
{ | |
loop=false; | |
} | |
else | |
{ | |
cout << "\n\tInvalid amount. Enter a integer: " ; | |
loop=true; | |
break; | |
} | |
} | |
} | |
istringstream in(str); | |
in >> x; | |
return x; | |
} | |
void editSpecies(Endangered animals[], int edit) | |
{ | |
bool loop=true; | |
while (loop) | |
{ | |
char species; | |
cout << "\n\tEdit Species: [B]=Bird, [F]=Fish, [M]=Mammal "; | |
cin >> species; | |
if (islower(species)) species=toupper(species); | |
switch (species) | |
{ | |
case 'B': | |
animals[edit].species=species; | |
cout << "\n\tSpecies changed to Bird."; | |
loop=false; | |
break; | |
case 'F': | |
animals[edit].species=species; | |
cout << "\n\tSpecies changed to Fish."; | |
loop=false; | |
break; | |
case 'M': | |
animals[edit].species=species; | |
cout << "\n\tSpecies changed to Mammal."; | |
loop=false; | |
break; | |
default: | |
cout << "\n\tInvalid selection! Please try again. "; | |
break; | |
}//end switch case | |
}//end While | |
} | |
bool againYN() | |
{ | |
char answer; | |
bool loop=true; | |
while (loop) | |
{ | |
cin >> answer; | |
if (islower(answer)) answer=toupper(answer); | |
if (answer=='N') | |
{ | |
return false; | |
loop=false; | |
} | |
else if (answer=='Y') | |
{ | |
return true; | |
loop=false; | |
} | |
else cout << "\n\tInvalid slection, try again: "; | |
} | |
} | |
void splashPage() | |
{ | |
string panthArray[24]; | |
panthArray[0]=" ___......----:'^^:--....(| "; | |
panthArray[1]=" .-':'"": : : : : : :.(1|.`'=b "; | |
panthArray[2]=" .'`. `. : : : : : : : : : : .';"; | |
panthArray[3]=" :-`. : . : : `. : : :. : :`.`. a;"; | |
panthArray[4]=" : ;-. `-.-._. : : : ::. .' `. `., = ;"; | |
panthArray[5]=" :-:.` .-. _-., : : : ::,.'.-' ;-. ,''' "; | |
panthArray[6]=" .'.' ;`. .-' `-.: : : : :;.-'.-.' `-'"; | |
panthArray[7]=" :. .'.'.-' .'`-.' -._;..:---''''~;._.-;"; | |
panthArray[8]=" :`--'.' : :' ;`-.; :.`.-'`. "; | |
panthArray[9]=" `''` : : ;`.; :=; `.-'`. "; | |
panthArray[10]=" : '. : ; :-: `._-`."; | |
panthArray[11]=" `''' `. `.., `--' `._;"; | |
panthArray[12]=" `'--''"; | |
cout << "\n\t *** ENDANGERED SPECIES LIST ***"<<endl; | |
cout << "\n\t COMP 130 Term Project - Spring 2013" << endl; | |
cout << "\n\t By Dana Muise"<< endl; | |
cout << "\n"<< endl; | |
for(int i=0; i<13; i++) | |
{ | |
cout << panthArray[i]<<endl; | |
} | |
cout << "\n\t Press any key to begin"<< endl; | |
_getch(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment