2019-01-19 09:54:32 (edited by Zarvox 2019-01-19 09:55:28)

I want to allow numbers 1 through 6, not allow 7, but also allow 8. How would I write this line? Also, how would I write a line that allows numbers 1 through 6, skips 7, and allows 8 through 10? Basically eliminating 7 from the list.

2019-01-19 10:18:30

You'd probably need a while loop. This is the situation for which do-while would be good, but for some reason everyone's trying to get rid of do-while these days.
So you'd first do the random number as usual, then on the next line, start a while loop that continues while the number is forbidden. In this case, while (num == 7)
This is the kind of loop I'd want to put on one line, but for readability:
int num = random (1, 10);
while (num == 7)
{
num = random (1, 10);
}
If you're curious, the do-while version would be like:
int num;
do { num=random (1, 10); }
while (num == 7);

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2019-01-19 11:33:09

When is a do while loop convenient? I have only used while loops

2019-01-19 12:59:37

Most people seem to agree. They're more useful when the script blocks for user input, which really shouldn't be done outside of text-based games. They also make more sense in older versions of C, when you'd declare all the variables at the start of a function, but not assign them values until later.
Mostly, if I have to copy a line of code to be both before and inside a while loop, I'd rather use a do-while. But this is not necessary.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2019-01-19 16:40:43

I dont remember full code but maybe you can random 1,6
8,last number?

Mao!
--
TD programs website available under new address.
https://tdprograms.ovh/

2019-01-19 18:14:47 (edited by Kyleman123 2019-01-19 18:16:36)

A do while loop is useful when you want something to run at least one time. fore example:

// this is c++ code
#include <iostream>
using namespace std;

void whileLoop(int x);
void doWhileLoop(int x);

int main()
{
    int x = 0;

whileLoop(x);
doWhileLoop(x);

    return 0;
}

void whileLoop(int x)
{
    // loop will check the condition and not run

    while (x > 0)
    {
        cout << "Running while loop. x is: " << x << endl; //
        x--; //decrement x
    }
}

void doWhileLoop(int x)
{
    // loop will run this time because it is a do while

    do
    {
        cout << "Running do while loop. x is: " << x << endl;
        x--; // decrement x
    }
    while (x > 0);
}

This is a bit contrived but i hope you get the point. do whiles can be very useful in many cases. They can help with debugging and protecting against your conditionals acting up because of weird input.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2019-01-19 18:45:04

there are many, many ways to do this. Using a while loop to check for a forbidden number is one, you can also create an array with the allowed numbers, then generate a random index. You could also, in your example, generate a random number between 1 and 7, and if it is 7, increase it by one. You can modify this example yourself to remove 7 from a random number between 1 and 10.

Roel
golfing in the kitchen