Looking for a few more efficient ways...

Picking up C++ again and to teach myself how to use order of operations and different function statements I hammered through a couple of hours of coding, but, i'm wondering, with the scope of what i'm doing, are there any points where there is a more efficient, smaller, or less sludgy way of doing things? Any shouts or pointers would be amazing.

Example below, and looking at it, the sheer number of if statements is scaring me considering I want to eventually add lots of people running through this same (and to be bigger) daily checklist.


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
151
152
153
154
155
156
157
158
/*
Zombie sim 0.1

An attempt to simulate the daily life of a human (broken down 24 hours in 24 minutes) to see the
odds of surviving on a per day basis and the expansion of the zombie populous vs the human populous

:D
*/
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <time.h>
using namespace std;

int main(){
    
//declare our variables
int awake, eat, safe, mate, survive, attempts, slept, ate, mated, survived, zombie, zombiefied, stupid, runs, eaten, starved;
string didsleep, dideat, aresafe, didmate, didsurvive, arezombie, starve;

//thrown here to work out how to do longest consecutive time spent alive
int chain, longest;

//zeroing out variables cause I know no better way
slept = 0;
ate = 0;
mated = 0;
survived = 0;
zombiefied = 0;
stupid = 0;
eaten = 0;
starved = 0;
chain = 0;
longest = 0;

//be polite, ask how long they want it to run first
cout << "How many attempts would you like to simulate? :>";
cin >> runs;

//loop with odds of one thing or the other happening
while( attempts < runs ){
awake = rand() % 3;
eat = rand() % 3;
safe = rand() % 3;
mate = rand() % 3;
survive = rand() % 3;
zombie = rand() % 5;
//+++++++++++++++++++++++++++++++++++++++++++
//are you awake? (will later determine if you more vulnerable to zombification
// due to being unconscious as well as whether or not you are forcefully
//put to sleep from exhaustion (how many days without sleep)
if( awake == 1 ){
    didsleep = "you did";
    slept++;}
else
    {didsleep = "you did not"; 
};
//+++++++++++++++++++++++++++++++++++++++++++
//Have you eaten today? Will move into how strong/weak you are from lack
//of nutrition (and overall food supply for when we start including
//larger numbers of people, also helps determine if you have starved to death
if( eat == 1 ){
    dideat = "you did";
    ate++;
    eaten = 0;
    }
else 
    {dideat = "you did not";
    eaten++;
};
//++++++++++++++++++++++++++++++++++++++++++++
//dunno why this is here yet, it seemed like an idea to have it but it might later
//determine your odds of encountering a zombie (and then determine your odds of surviving
//that encounter (i.e. 1 zombie = good odds, unsafe = 100's of zombies = death T_T
if(safe == 1){
    aresafe = "you are";}
else
    {aresafe = "you are not";
};
//+++++++++++++++++++++++++++++++++++++++++++++
//er... im assuming the human race is going try to keep going, which means the population
//will fluctuate....
if(mate == 1){
    didmate = "you did";
    mated++;}
else
    {didmate = "you did not";
};
//+++++++++++++++++++++++++++++++++++++++++++++
//well? did you?!? also helps determine how long you stayed alive
if (( survive == 1 ) && ( eaten < 14 )){
    didsurvive = "you did";
    survived++;
    chain++;}
    
else
    {didsurvive = "you did not";
    chain = 0;
};

if(chain > longest)
longest++;
//++++++++++++++++++++++++++++++++++++++++++++++
//Have you joined the undead horde?
if( zombie == 1){
    arezombie = "you are";
    zombiefied++;}
else
    {arezombie = "you are not";
};
//+++++++++++++++++++++++++++++++++++++++++++++++
//its here that we determined how many  attempts you have failed at eating, 
//assuming overall attempts were 3 times daily and 14 days till starvation... or
//was that dehydration o_O
if( eaten > (13 * 3) ){
    starve = "starved";
    starved++;}
else
    {starve = "did not starve";};
    

    
//daily statistics
cout << "Welcome to your day!" << endl;
cout << "Today: " <<endl;
cout << didsleep << " sleep..." << endl;
cout << dideat << " eat..." << endl;
cout << aresafe << " safe..." << endl;
cout << didmate << " mate..." << endl;
cout << didsurvive << " survive..." << endl;
cout << "And " << arezombie << " a zombie!" << endl;
cout << endl;


attempts++;
};

//global statistics after it has finished the overall loop
cout << "In total you:" << endl;
cout << "Slept " << slept << " times" << endl;
cout << "Ate " << ate << " times" << endl;
cout << "Mated " << mated << " times" << endl;
cout << "And survived " << survived << " times!" << endl;
cout << "You starved " << starved << " times!" << endl;

//if you didnt die from old age or natural death, and if you werent zombified
//or starved to death, you obviously did something you are less then comfortable
//admitting and died cause of it =D
stupid = runs - survived - zombiefied - starved;

cout << "You died " << stupid << " times from idiocy" << endl;
cout << "Sadly, you became a zombie " << zombiefied << " times T_T" << endl;
cout << "Your longest run was " << longest << " days alive!" << endl;

return 0;
}


Last edited on
One thing I recommend is reducing the number of variables you're using. It's killing me just to look at lines 18-19. There must be a better way to do this.

You can't declare all of your variables as integers. For a string you must use a character array.
Do you mean you plan on having multiple virtual people in the game? I'd suggest looking into classes so that it's easier to manage. Since you want to keep track of a variety of statistics (ie, # times slept/ate/died?/zombified?(cant those only happen once?)/mated etc), I think it's unavoidable using all those variables.

Also, strings, "things in quotation marks", can't be put into int variables. You would need to use either a string or a character array (I'd recommend strings because they're far easier to use if you don't really need to manipulate the words so much)

You should also try using functions to clean up the code. Having everything in main is a headache.

Combining functions with classes, your main() might look something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    Person people[100];
    int deathcount = 0;

    while (true)
    {
        for (int i=0; i<100; i++)
        {
            people[i].Update();
            people[i].PrintStatus();
            if (people[i].isDead) 
                deathcount++;
        }
        // then, 
        if (deathcount == 100) 
                break;
    }
    return 0;
}
Yes indeed i do ultimately intend it to grow in virtual person size. I was afraid of the multi-variable kludge being necessary >_< zeroing all those out is really sucking.

As for the string suggestion, my above code does indeed declare strings o.o
line 19.
1
2
3
//declare our variables
int awake, eat, safe, mate, survive, attempts, slept, ate, mated, survived, zombie, zombiefied, stupid, runs, eaten, starved;
string didsleep, dideat, aresafe, didmate, didsurvive, arezombie, starve;


Also, I guess its time to bust into learning how do do classes. I've never done them so it'll be an experience i'm sure.

Since each person has to individually go through the exact same checklist of if() statements, would it be easier to just group all the if's into a class then, i guess, call the class and feed the persons identifying marker into it to see if that one person survived, then move onto the next person and repeat?

[edit] Just meandered into the Functions(I) and (II) tutorials and in thinking im grasping how to condense alot of this.
Last edited on
Topic archived. No new replies allowed.