심심해서 로또번호 생성기를 만들어 봤는데 가끔씩 중복된 번호가 뜨네요...
맨위 Lotto 번호를 보시면 8이 두번나옵니다. 코드를 한번만 봐주시고 뭘 잘못했는지 좀 힌트해 주세요...
그리고 c에서 c++로 넘어가려면 c를 어느정도 해야되나요?
#define _CRT_SECURE_NO_WARNINGS
#define LOTTO 7
#define STRIKE 4
#include <stdio.h>
#include <time.h>
#include <string.h>
int main(void){
int powerball;
int strike[STRIKE] = {0};
int lotto[LOTTO] = {0};
int lottoCounter, strikeCounter, i;
int randomLotto, randomStrike;
int drawAgain = 0;
char ask[10];
srand(time(NULL));
// keep repeating the draw until the user wishes to quit //
while (!drawAgain){
// generate the first lotto number //
lotto[0] = 1 + rand()%45;
lottoCounter = 1;
// use while loop to get another lotto number. however if the number is same as stored number, draw again //
while (lottoCounter < LOTTO){
randomLotto = 1 + rand()%45;
for (i = 0; i < lottoCounter; i++){
if (lotto[i] != randomLotto){
lotto[lottoCounter] = randomLotto;
}
}
lottoCounter++;
}
// generate first strike number //
strike[0] = 1 + rand()%45;
strikeCounter = 1;
// use while loop to get another strike number, however if the number is same as stored number, draw again //
while (strikeCounter < STRIKE){
randomStrike = 1 + rand()%45;
for (i = 0; i < strikeCounter; i++){
if (strike[i] != randomStrike){
strike[strikeCounter] = randomStrike;
}
}
strikeCounter++;
}
// generate random powerball number //
powerball = 1 + rand()%10;
// display the lotto, strike, and the powerball number //
printf("Lotto: ");
for (i = 0; i < LOTTO; i++){
printf("%d ", lotto[i]);
}
printf("\n");
printf("Strike: ");
for (i = 0; i < STRIKE; i++){
printf("%d ", strike[i]);
}
printf("\n");
printf("Powerball: %d\n", powerball);
// ask the user for another draw //
printf("Another draw? Please enter yes or no: ");
scanf("%s", ask);
printf("\n");
if (strcmp(ask, "yes") == 0){
drawAgain = 0;
} else {
drawAgain = 1;
}
}
return 0;
}