Home > Back-end >  What is wrong with this lottery Input - Raffle C code?
What is wrong with this lottery Input - Raffle C code?

Time:11-09

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() E   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;
}

CodePudding user response:

As other have already said, the code is a mess, since there's no identation, so it's quite difficult to understand and there's a high risk of making errors in the code. Check out here: https://codehs.gitbooks.io/introcs/content/Programming-with-Karel/how-to-indent-your-code.html
Did you use something like the old version of Dev C to write the code? I've seen others struggling with that.
I would also suggest you to not specify array size as a number: e.g int array[7]; but to define a constant int this way:
#define array_size 7
int array[array_size];
This will let you to modify your program without the need to rewrite all the code and also will prevent you to have some bugs, and you should give self describing names to your variables.

#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]);

I think that the problem is here, since you are comparing values that still don't exist, the second for cycle and the third one are executed before the array yours if fully initialized. To fix it you have to change their conditions

    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() E   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; //What does it mean?

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

return 0;

}

  • Related