#include <iostream>
usingnamespace std;
int main()
{
// assume this is my structure array, all works the same
char vowels[5] = { 'a', 'e', 'i', 'o', 'u' };
// number of letters, just like your number of creatures.
int letterCountMax = 5;
// used by the do..while to move through the array
int index = 0;
// assume this i was passed using cin to letter variable :)
char letter = 'i';
// simple check to see if we found something.
bool found = false;
do
{
// have we got the letter we are looking for?
if (vowels[index] == letter) {
// ive found the letter :)
// create a loop starting from the index where
// my letter was found to the last element stored
// in letterMaxCount - 1 (i.e 0-4 = 5 chars)
for (int i = index; i < letterCountMax; i++)
vowels[i] = vowels[i + 1];
// since we removed one, deduct the number of
// elements (like your creatures one)
letterCountMax--;
found = true; // found it.
// break out the loop
break;
}
else
index++; // move up
} while (index < letterCountMax);
if (!found)
cout << endl << "Couldnt find what you were looking for..." << endl << endl;
else
{
// lets just show what we have now
for (int i = 0; i < letterCountMax; i++)
cout << "Letter at index " << i << " is " << vowels[i] << endl;
}
return 0;
}
Your deleteCreature shows the list of current names, asks the question for which name to remove then calls the moveArrayElements function to do the moving. The example I gave you would be in the moveArrayElements
//
// $function: deleteCreature
//
int deleteCreature(Creatures data[], int numCreatures)
{
string name;
int creatures;
if (numCreatures == 0)
return 0;
creatures = numCreatures;
cout << endl << endl << "The following is a list of all the creatures you take care of:" << endl;
for (int i = 0; i < numCreatures; i++)
cout << data[i].name << endl;
cout << endl << "What creature would you like to remove? ";
getline(cin, name); cin.sync();
// remove it.
if (moveArrayElements(data, numCreatures, name)) {
cout << endl << "You have removed " << name << endl;
creatures--; // we are one less as we removed 1
}
return creatures; // update with new creatures count
}
int deleteCreature(int numCreatures, Creatures profile[])
{
string name;
int creatures;
creatures = numCreatures;
if (numCreatures == 0)
{
return 0;
}
cout << endl << endl << "The following is a list of all the creatures you take care of:" << endl;
for (int i = 0; i < numCreatures; i++)
{
cout << profile[i].name << endl;
}
cout << endl << "What creature would you like to remove? ";
getline(cin, name); cin.sync();
if (moveArrayElements(numCreatures, name, profile)) {
cout << endl << "You have removed " << name << endl;
creatures--;
}
return creatures;
}
bool moveArrayElements(int numCreatures, name, Creatures profile[]) //error to do with name here.
{
int index = 0;
int creatureMaxCount = numCreatures;
int i = 0;
bool wasMoved = false;
do
{
if (profile[i] == name)// some error to do with this is making cmd angry
{
for (i = index; i < creatureMaxCount; i++)
profile[i] = profile[i + 1];
creatureMaxCount--;
wasMoved = true;
return wasMoved;
break;
}
else
index++;
} while (index < creatureMaxCount);
}
if (profile[i] == name)// some error to do with this is making cmd angry
{
for (i = index; i < creatureMaxCount; i++)
profile[i] = profile[i + 1];
creatureMaxCount--;
wasMoved = true;
return wasMoved;
break;
bool moveArrayElements(int numCreatures, string name, Creatures profile[])
{
int index = 0;
int creatureMaxCount = numCreatures;
do
{
if (profile[i].name == name)
{
for (int i = index; i < creatureMaxCount; i++)
profile[i] = profile[i + 1];
creatureMaxCount++;
returntrue;
}
else
index++;
} while (index < creatureMaxCount);
returnfalse; // if we get here nothing was found.
}
if (profile[i].name == name) //only error is left here the i in this part isn't declared.
i wrote half this code on my tablet because my laptop died and i had to search all over for a hdd and a copy of windows, and could not spare the time to stop because its due thursday.
Remember the finer details for your assignment, i.e. the structure should be in prog4.h header file and not in your cpp file... just minor things but still contribute to marks.
Well i have some more issues and ill try to compile them all, then post them if i cant figure them out myself. Enjoy your night good sir, if your head doesn't hurt too bad in the morning id appreciate acquiring your assistance yet again.
meh i don't suffer from hangovers, I'm a big boy now ;-)
Before you head off to bed tonight post you program again, either here or by private message.. don't mind either way and I'll take a look in the morning. remember the time difference though I'm in England
Good luck with the coding, i can see already you have come a long way.