Remainder Challenge in C++

I wrote a small C++ Programm for this. Any recommendations on what I could do better?

#include ;
#include <stdlib.h>;
using namespace std;

void main()
{
int rn;
rn = rand() % 100;

cout << rn << " " << rn / 4 << " " << rn % 4 << endl; // To Check Numbers

if (rn % 4 == 0)
{
	cout << "Player 1" << endl;
}
else if (rn % 4 == 1)
{
	cout << "Player 2" << endl;
}
else if (rn % 4 == 2)
{
	cout << "Player 3" << endl;
}
else if (rn % 4 == 3)
{
	cout << "Player 4" << endl;
}
else
{
	cout << "something went terribly wrong" << endl;
}

system("pause");

}

1 Like

What exactly are you trying to do?

Hi @Bitlive,
If you only want to print out which player goes first and nothing else, you can condense your if-else statements to just a single line.

And to make things more readable and expandable, you may also want to create some variables that you can reuse later.

This will leave you with something like the following, where you only need to set the playerCount and everything else will adjust accordingly;

int playerCount = 4;
int remainder = rn % playerCount;
int startingPlayer = remainder + 1;

cout << rn << " " << rn / playerCount << " " << remainder << endl; // To Check Numbers

cout << "Player " << startingPlayer << endl;

system("pause");

Note: My C++ is rusty at best, so hopefully it works as expected :slight_smile:

Privacy & Terms