Mucking about (should say practising what I've learn't), and made my version of the rabbits practice program. It's not totally finished but it's my first draft and it's working. Before continueing, I'd like a code review please to tell me whats not correctly done or how I could improve my coding? System pause, I know, and it will be out later on.
Here's my code

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int mr = 1, fr = 1, br = 0;             //mr = male rabbits //fr = female rabbits //br = baby rabbits
    int pr = 0;                             //pr = pregnant rabbits
    int total = 0;
    int temp = 0;
    int flag1 = 0;

while (total <= 1000){    

        // one off population cull
    if (total >=500 && flag1 == 0)
        {
        cout << "Disease strikes the colony!" << endl;
        flag1 = 1;
        mr = mr / 4;
        fr = fr / 4;
        br = br / 4;
        pr = 0;
        }

    if (br > 0)                        //if any babies
        {
        temp = br / 2;                 
        mr = mr + temp;                //adds 1 to male population
        fr = fr + temp;                //adds 1 to female pop
        br = 0;
        temp = 0;                      //resets temp value to 0
        }

    if (pr > 0)                        //if any rabbits pregas
        {
        br = pr * 2;                   //2 babies per pregnant rabbit
        pr = 0;                        //resets pregas to 0
        }
    else
        

    if (mr == fr)
        pr = fr;                       //puts each female pregnant
          
cout << "There is " << mr << " male rabbit" << endl;
cout << "There is " << fr << " female rabbit" << endl;
cout << "Babies born x " << br << endl;
cout << pr << " Female is now pregnant" << endl;

total = mr + fr + br;
cout << "Total Rabbits = " << total << "\n" << endl;

}                     //while loop closing brace

    system("PAUSE");	
    return 0;
}

Thanks in advance
Leppie

Have you run your program and verified that the result is as intended? Will the program ever complete running?

There shouldn't be any problem with the code you posted.

Thanks chiwawa10.
Yes it does run and completes as expected. Just wondered if it was neat coding or not? (I didn't like the "mr = mr + temp" and thought there might have been a better way to do this?)
I might get back to you all later though as my next little addition is to add random sex for the birth of the babies and possibly create a percentage chance for a mutating radioactive bunny that will start decimating the population on every run.

You could make it OO I guess? Something that simple doesn't really need it though but if you want to do it as an exercise, try creating classes for the types of rabbits.

You could make it OO I guess? Something that simple doesn't really need it though but if you want to do it as an exercise, try creating classes for the types of rabbits.

I totally agree with that. C++ take its power from its OO paradigm. Other than that, it's actually a C code except cout :)

Oh, and a simple solution for you system(pause), just throw in cin.ignore, and then right below that, cin.get.

didn't like the "mr = mr + temp" and thought there might have been a better way to do this?)

mr += temp; // equivalent to your code above
mr = mr + temp;                //adds 1 to male population
        fr = fr + temp;                //adds 1 to female pop

The comments don't match the code here. If it adds 1, have it add 1. If it doesn't add 1, fix the comment so that it matches the code.

I don't think this is a problem, really, and it's a little trivial, but since you are using C++, the C++ header is cstdlib rather than stdlib.h.

Aside from that, I'd say you could make the indentation a bit more consistent.

Edited 6 Years Ago by VernonDozier: n/a

Thank you all so much, for your comments and recomendations. (obviously still got a lot to learn and I appreciate all your assistance). I can now update my headers, finally get rid of system Pause and fix that horrible mr = mr + temp. I will try to indent better too. I will get to OO to eventually!

Thanks

This question has already been answered. Start a new discussion instead.