Detecting and Excluding the Lowest Number.

Hello!

Currently, I"m trying to make a stat roller for D&D DMs to make NPCs faster and easier.

Thus far, I've managed to generate, add, and display four numbers and their sum.
However, I'm trying to detect whichever is the lowest number, and then exclude it from the sum. I imagine that I need to NULL it somehow.

From my code, you can see that I've made all four numbers floats.

I appreciate any help rendered, and that you all stay happy and safe.

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main() {
srand((unsigned) time(0));
float randomNumber1, randomNumber2, randomNumber3, randomNumber4, tot;
for (int index = 0; index < 1; index++) {
randomNumber1 = (rand() % 6) + 1;
randomNumber2 = (rand() % 6) + 1;
randomNumber3 = (rand() % 6) + 1;
randomNumber4 = (rand() % 6) + 1;
tot=randomNumber1 + randomNumber2 + randomNumber3 + randomNumber4;
cout << randomNumber1 << endl;
cout << randomNumber2 << endl;
cout << randomNumber3 << endl;
cout << randomNumber4 << endl;
cout << randomNumber1 << " + " << randomNumber2 << " + " << randomNumber3 << " + " << randomNumber4 << " = " << tot << endl;
}
}
int lowest;
lowest = randomNumber1 = (rand() % 6) + 1;
randomNumber2 = (rand() % 6) + 1;
if(randomNumber2 < lowest) lowest = randomNumber2;
repeat...
add them up, subtract lowest, and you have the sum of the highest 3 of 4.

its inefficient but less code you can also store the 4 values in a vector / array, sort them, and use the 3 highest.
Last edited on
@Jonnin

Thanks, dude! I'll get right on it!
In the future, use code tags, please.
http://www.cplusplus.com/articles/jEywvCM9/
@TheToaster

Thank you for the article, I'll do so from now on.

As an aside, the preview function doesn't seem to be working.
I missed it first time. float does not do anything here, just use int. rand() returns an int.

Topic archived. No new replies allowed.