My code isn't doing what I want it to. It runs through the code twice for some reason. What do?

My code isn't doing what I want it to. It runs through the code twice for some reason. What do?

Start GDB

do while()

This makes no difference. I think it's because after the first getchar there's two characters in the buffer so on the second time through it uses the second one.

Try using a continue to see if it works

correct answer.
you need to clear the buffer

>Sup Forums - Homework Help

I did, didn't work.

Not homework, it's from picrel

How do I clear the buffer?

This.

Everyone knows that. Most common error in the world.

while(1) {...}

Is an infinite loop. Put a break statement in there too.

r%3?

Try R*3.
fag

I need a random number between 0 and 2, for the three options in the game. r%3 achieves this nicely.

Indeed. And?

>How do I clear the buffer?

I dont know C, so I cant help you with that.
C++ has a std class function for doing it though I believe so its easy for us.

>hurr

Found it

>(){

Get the fuck out.

You do c = getchar();
I think you put in s and press return.
It reads both the s and the return.
I suggest you use scanf

>How do I clear the buffer?

I know for at least in scanf, the program would register the enter key as a '\n' in the buffer, and it would be read if you had the format parameter for scanf set to %c. You could prevent that by writng scanf("\n%c" , &variable)

>coding in C over C++ for simple applications

What the fuck are you doing? Stop listening to Linus and use C++14.

#include
#include

int main()
{
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution normdist3(1,3); // distribution in range [1, 3]
bool gaming=true;
while(gaming){
int AI_hand=normdist3(rng);
int player_hand;
char c;
std::coutc;
switch(c){
case 'r':
std::cout

That's pretty damn sexy
Could you explain the first 2 lines of main?

/thread

Seriously OP, if you're going to use C, take the time to learn GDB. When you fire it up, run tui enable. Then follow RMS's gdb tutorial.

>std::mt19937 rng;

Your pseudoRandom Number Generator, in this case Mersenne Twister based on the Mersenne prime 2^19937−1

>rng.seed(std::random_device()());

Seeds the random number generator with a source of randomness. Time is shitty source of randomness in real code.

>std::uniform_int_distribution normdist3(0,2); // distribution in range [0, 2]

Normal distribution where number 0, 1, 2 have equal probability of appearing. Using %n is a shitty way of faking a normal distribution and will bias some numbers over others.

You can also bind them together with auto rolling = std::bind ( normdist3, rng); so you can just call AI_hand=rolling();

>calling normal and uniform the same thing

Go to bed.

>Using %n is a shitty way of faking a normal distribution and will bias some numbers over others.
Will it though? Over a large range there should be roughly equal number of each remainder, plus or minus 1 for each possibility.
i.e. in the range of 12 for modulo 3, you get 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0.
There's one extra zero, but if you do it over 30,000 then said variance is only 1 in 10,000.
Not perfectly uniform but very close.

>else if else if else if
>if then a = ...else if then a = ... etc
This smells like its the first code youve ever written

also
>what is string deduplication/formatting
>what is translation of user input to bounded values/constants instead of basing control flow directly on input

Wanna rewrite a fixed version? Its not my first code but I was just doing it as quick as possible