I'm writing a piece of program where I use '1 + rand() % 10' to generate numbers between 1 and 10. Rand() function is followed by an if and if else statements.
The program should output different incremented values every second, but is instead only incrementing as per the first if condition (by 3). ONLY the first if statement is executed, else if and else are NEVER considered. Even when random number fails the test, the first if statement executes despite that.
Why? It all starts and stops at the first if condition.
I have tried to debug with inserting 'cout' and also with 'break points' in gdb debugger. Couldn't see the nitty gritty details enough to find the bug.
if(1 <= r && r <= 5)
{
return previous + 3;
}
if(6 <= r && r <= 7)
{
return previous - 6;
}
return previous + 1;
It might be that (1 <= r) condition is only seen and all the numbers fulfill this. Also no point in the else statements as you have a return in each case