C++ multiple random numbers adding up to equal a certain number

后端 未结 3 1115
时光取名叫无心
时光取名叫无心 2021-01-29 08:15

Having a little trouble figuring this one out, I have a bunch of int variables which are generating random numbers from 1-9. I need them to generate the correct numbers to equal

3条回答
  •  误落风尘
    2021-01-29 08:49

    Move the rand() into the while loop, or you'll generate random numbers only once, and get stucked in while loop.

    do{
    int numone = rand()%10;
    int numtwo = rand()%10;
    int numthree = rand()%10;
    int numfour = rand()%10;
    int finalsum;
    finalsum = numone + numtwo + numthree + numfour;
    }while(finalsum !=30);
    

提交回复
热议问题