Code skipping - function problem?
May 9, 2015 at 1:26am UTC
I'm trying to write a rabbit cancer sim and I am having an issue: my program stops at "starting simulation" on line 88. I looked up all of my syntax and I see no errors. I want the program's main loop to function after my switch statement. Should I move my switch to another function to fix this? I'm using dev-c++ if that's relevant.
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
#include <cmath>
void sleep(long waittime);
int startup();
void babyCount();
void babySort(int babies);
void setup();
void printRules();
void infoOut();
void breakTime( float seconds)
{
clock_t temp;
temp = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < temp) {}
}
using namespace std;
long int m,f,z;
int y,a = 0;
int main()
{
int initial = startup();
switch (initial)
{
case 1:
printRules();
setup();
break ;
case 2:
setup();
break ;
case 0:
return 0;
break ;
default :
cout << "Error - Closing Program" << endl;
return 0;
}
int exit;
while (exit != 1)
{
babyCount();
infoOut();
breakTime(1);
}
}
int startup()
{
cout << "Rabbit Zombies" << endl;
cout << "For Rules Press 1:" << endl;
cout << "For Setup Press 2" << endl;
cout << "To Quit Press 0" << endl;
int q;
cin >> q;
return q;
}
void printRules()
{
cout << "RULES:" << endl;
cout << "1.) For every Male/Female rabbit pair a new rabit is created yearly" << endl;
cout << "2.) Zombie rabbits infect other rabbits twice yearly" << endl;
cout << "3.) There is a 5% chance a rabbit born will be a zombie" << endl;
}
void setup()
{
cout << "Enter Starting Ammount of Male Rabbits: " << endl;
cin >> m;
cout << "Enter Starting Ammount of Female Rabbits: " << endl;
cin >> f;
cout << "Enter Starting Ammount of Zombie Rabbits: " << endl;
cin >> z;
cout << "Starting Simulation ... " << endl;
}
void babySort(int babies)
{
while (babies != 0){
breakTime(0.1);
srand( time(0));
switch (rand()%20+1)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 7:
case 8:
case 9:
case 10:
m++;
break ;
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
f++;
break ;
case 20:
z++;
break ;
}
babies--;
}
}
void babyCount()
{
if (m > f) { a = f; }
if (m < f) { a = m; }
if (m == f) { a = f; }
babySort(a);
}
void infoOut()
{
cout << "The year is: " << y << endl;
cout << "The Male Population is :" << m << endl;
cout << "The Female Population is: " << f << endl;
cout << "The Zombie Population is : " << z << endl;
}
May 9, 2015 at 3:10am UTC
The problem may not be syntax but maybe a logic based problem. One thing you could do is go in order of your program and check what how it goes.
May 9, 2015 at 4:14am UTC
Look closer at line 51 and 52
May 9, 2015 at 7:07pm UTC
Look closer at line 51 and 52
and
The problem may not be syntax but maybe a logic based problem. One thing you could do is go in order of your program and check what how it goes.
thank you so much! I need to get better at examining my flow and to stop adding little pieces of code to test things lol.
Topic archived. No new replies allowed.