I could really use some help with this program

Hello all,

I have spent the last few very late nights trying to figure out this code. My instructor for my intro to programming course is having us create a game for our final project. The game is pretty basic and he wanted us to use a variety different functions and loops. It is a game about Ants and mostly uses random number generators to decide the outcome. For the most part, it is currently exactly the way he wanted it.

I am mainly having difficulties with the getMyFood() function. I first noticed the problem when the function would run twice within the same loop pass. I basically added a final else command with a cout message at the bottom of my function. I should never receive the message because I am only generating a random number between 1&3. I test the random number to see if its 1,2 or 3, and if its not my last else cout command kicks in. This only happens about every other game or so which has me even more confused that it is not a consistent error.

Here is the code:

#include <iostream>
#include <string>
#include <time.h>
#include <windows.h>
using namespace std;

void beginGame(); //This is the function that starts the game

string dayOfWeek(int dayNum);
void hatchBabies(int & myAnts, int & enemyAnts, int & myBabies, int & enemyBabies);
void gameStats(int myColony, int enemyColony, string date);
string day;
void getMyFood(int & myFood, string & myFoodType);
void getEnemyFood(int & enemyFood, string & enemyFoodType);
void getFood(int & myFood, int & enemyFood, string & myFoodType, string & enemyFoodType );
void willFight(int & myAnts, int & enemyAnts);
bool willMeet();


int main() {

srand(time(0));
beginGame();

system ("pause");
return 0;
}

string dayOfWeek(int dayNum) {

switch (dayNum)
{
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
case 7:
day = "Sunday";
default:
break;
}

return day;

}

void beginGame()
{
string currentDay;
string myFoodType;
string enemyFoodType;
int myAnts = 100;
int enemyAnts = 100;
int myBabies = 100;
int enemyBabies = 100;
int myFood = 0;
int enemyFood = 0;


for (int i=1; i<8; i+=1)
{
currentDay = dayOfWeek(i);
gameStats(myAnts, enemyAnts, currentDay);
hatchBabies(myAnts, enemyAnts, myBabies, enemyBabies);
cout<<endl;
cout<<myBabies<<" ants were hatched in my colony today. My colony now has " <<myAnts<< " ants."<<endl;
cout<<enemyBabies<<" ants were hatched in enemy colony today. Enemy colony now has " <<enemyAnts<< " ants."<<endl;
cout<<endl;
getMyFood(myFood, myFoodType);

if (myFoodType =="nothing")
{
myAnts = myAnts - myFood;
cout <<"My colony searches for food and finds "<<myFoodType<<"."<<endl;
cout <<myFood<<" ants die of starvation.";
cout <<"The colony now has "<<myAnts<<" ants."<<endl;
cout <<endl;
}
else if (myFoodType =="dead bug")
{
myAnts = myAnts + myFood;
cout <<"My colony searches for food and finds "<<myFoodType<<"."<<endl;
cout <<"Crunchy! They devour it and add "<<myFood<<" ants due to the nutrition. "<<endl;
cout <<"The colony now has "<<myAnts<<" ants."<<endl;
cout <<endl;
}
else
{

myAnts = myAnts + myFood;
cout <<"My colony searches for food and finds "<<myFoodType<<"."<<endl;
cout <<"Sweet! They devour it and add "<<myFood<<" ants due to a sugar rush. "<<endl;
cout <<"The colony now has "<<myAnts<<" ants."<<endl;
cout <<endl;
}


getEnemyFood(enemyFood, enemyFoodType);

if (enemyFoodType =="nothing")
{
enemyAnts = enemyAnts - enemyFood;
cout <<"The enemy colony searches for food and finds "<<enemyFoodType<<"."<<endl;
cout <<enemyFood<<" ants die of starvation."<<endl;
cout <<"The enemy colony now has "<<enemyAnts<<" ants."<<endl;
cout <<endl;
}
else if (enemyFoodType =="dead bug")
{
enemyAnts = enemyAnts + enemyFood;
cout <<"The enemy colony searches for food and finds "<<enemyFoodType<<"."<<endl;
cout <<"Crunchy! They devour it and add "<<enemyFood<<" ants due to the nutrition. "<<endl;
cout <<"The enemy colony now has "<<enemyAnts<<" ants."<<endl;
cout <<endl;
}
else if (enemyFoodType == "candy bar")
{

enemyAnts = enemyAnts + enemyFood;
cout <<"The enemy colony searches for food and finds "<<enemyFoodType<<"."<<endl;
cout <<"Sweet! They devour it and add "<<enemyFood<<" ants due to a sugar rush. "<<endl;
cout <<"The enemy colony now has "<<enemyAnts<<" ants."<<endl;
cout <<endl;
}
else{cout<<"this is a mistake"<<endl;}

if (willMeet()==true)
{
cout<<"The colonies run into each other while returning home. What do you want to do?"<<endl;
willFight(myAnts, enemyAnts);
}

cout<<"My colony returns safely home for the night."<<endl;


system("pause");
system("cls");



}
}

void hatchBabies(int & myAnts, int & enemyAnts, int & myBabies, int & enemyBabies)
{
myBabies = 1 + rand() % (25 - 1 + 1);
enemyBabies = 1 + rand() % (25 - 1 + 1);
myAnts = myAnts + myBabies;
enemyAnts = enemyAnts + enemyBabies;
}

void gameStats(int myColony, int enemyColony, string date)
{
cout << "Today is "<<date<<endl;
cout << "My Colony : "<<myColony<<endl;
cout << "Enemy colony : "<<enemyColony<<endl;


}

void getMyFood(int & myFood, string & myFoodType)
{
myFood = 1 + rand() % (3 - 1 + 1);

if (myFood == 1)
{
myFood = 5;
myFoodType = "nothing";
}
else if (myFood == 2)
{
myFood = 5;
myFoodType = "dead bug";
}
else if (myFood == 3)
{
myFood = 10;
myFoodType = "candy bar";
}
else{cout<<"Something is wrong with getMyFood()"<<endl;}
}

void getEnemyFood(int & enemyFood, string & enemyFoodType)
{
enemyFood = 1 + rand() % (3 - 1 + 1);

if (enemyFood == 1)
{
enemyFood = 5;
enemyFoodType = "nothing";
}
if (enemyFood == 2)
{
enemyFood = 5;
enemyFoodType = "dead bug";
}
else if (enemyFood == 3)
{
enemyFood = 10;
enemyFoodType = "candy bar";
}
else{cout<<"Something is wrong with getEnemyFood()"<<endl;}

}

bool willMeet()
{

int meetingChance = 1+(rand() %2);

if (meetingChance == 1)
{
return true;
}

return false;

}

void willFight(int & myAnts, int & enemyAnts)
{
char choice;
int myDamage = 1 + rand() % (myAnts - 1 + 1);
int enemyDamage = 1 + rand() % (enemyAnts - 1 + 1);

do {
cout << "(F)ight or (R)un away: "<<endl;
cin >> choice;
choice = toupper(choice);

if (choice == 'F')
{
cout << "My colony attacks and causes damage of "<<myDamage<<endl;
cout << "The enemy colony attacks and causes damage of "<<enemyDamage<<endl;

if (myDamage > enemyDamage)
{
cout << "My colony wins the fight. The enemy colony loses 10 ants in the battle."<<endl;
enemyAnts = enemyAnts - 10;
}
else if (myDamage < enemyDamage)
{
cout << "My colony loses the fight, and loses 10 ants in the battle."<<endl;
myAnts = myAnts - 10;
}
else
{
cout << "We fight to a draw."<<endl;
}
}

if (choice == 'R')
{
cout << "My colony retreats, but we lose 5 ants while running away."<<endl;
myAnts = myAnts - 5;
}


} while (!choice == 'F' && !choice == 'R');

}
Last edited on
Topic archived. No new replies allowed.