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

后端 未结 3 1085
时光取名叫无心
时光取名叫无心 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:55

    Well, i hope you realize that in your do while loop, you are not actually changing the values of the random numbers.What you are doing is basically checking the same condition of finalsum = numone + numtwo + numthree + numfour; infinitely if it does not equal 30.What you should do is:

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

提交回复
热议问题