mercredi 9 novembre 2022

What is wrong with this lottery Input - Raffle C code?

What is wrong with this lottery Input - Raffle code?

I have been working on a code that when I input 7 numbers by scanf (1~45 integer), 7 numbers are randomley picked, the 2 sets of 7 numbers (mine and the random one) are compared, and the program outputs how much of my numbers are the same with the randomley picked ones. (the order of numbers doesn't matter) If I input more than one same numbers, for expample, 1 1 2 3 4 5 6, or input a number larger than 45, an error message must be printed. If both of these errors are true, there should be a seperate error message.

What is wrong with this lottery Input - Raffle code?

I have been working on a code that when I input 7 numbers by scanf (1~45 integer), 7 numbers are randomley picked, the 2

When I run the program, the radom picking of numbers (raffle) seems to be working fine, no overlapping numbers. the counting of matching number works as well. what is weird is the error messages. since the counting works, I assume the input values are well saved, but the program always outputs multiple lines of (7, exactly) "You cannot choose same number" . can anyone help me with this problem? The source code is below.

#include <stdio.h>
#include <stdlib.h>

int main () {

int yours[7];
printf("Buy yours: ");
for (int i=0; i<7; i++){

scanf("%d", &yours[i]);

for (int j=0; j<7; j++){
for (int k=0; k<7; k++){

if (yours[j] == yours[k] && yours[k]>45){
printf("You cannot choose same number and number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
}

else if (yours[j]>45){
printf("You cannot choose number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
break;
}

else if (yours[j] == yours[k]){
printf("You cannot choose same number.");
printf("\n\n");
printf("Buy yours: ");
break;
}
break;
}
break;
}

}
printf("Lottery result: ");
int lottery[7];

for (int i=0; i<7; i++){
lottery[i] = rand() %45 + 1;

for (int j=0; j<i; j++){

if (lottery[i] == lottery[j]) {
i--;
break;
}
}
}

for (int k=0; k<7; k++){
printf("%d ", lottery[k]);
}

int a = 0;

for (int i=0; i<7; i++){
for (int j=0; j<7; j++){
if (lottery[i] == yours[j]) {
a++;
}
}
}
printf("\n");
printf("The number of yours : %d", a);

return 0;
}



Aucun commentaire:

Enregistrer un commentaire