문제

This is my assignment.

  1. If random 3 digit number matches user three digit in exact order then some awards.
  2. If user matches 3 digit number but not in order, then some other awards.
  3. If user matches 2 digit, then some other prize
  4. If user input matches just 1 digit, then some other prize

This is what I came up with:

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

int main(){
    int r,i,num[0];
  srand(time(NULL));
  r = rand()%(999-100+1)+100;

  printf("Here is the wininng number%d\n",r);

  printf("Enter three digit number to win lottery:\n");
  scanf("%d",num);

  for(i=0;i<3;i++){

    if(r==num[0]){
        printf("For three exact match you get $100,000\n");
    }else if((r/10)==(num[1]/10)){
        printf("For two number match you get $50,000\n");
    }else if((r%10)==(num[]%10)){
        printf("For one number match you get $10,000\n");
    }else{
        printf("You get nothing!\n");
    }}

}

I get like three digit match, and some time three digit and two digit match after compiling.Show me what's wrong. Thanks you guys in advance.

도움이 되었습니까?

해결책

Your logic is wrong.

A 3-digit exact match is found by comparing what they entered with what you generated (the winning number).

A 3-digit permutation is trickier, but you also need need to account for 2-digit and 1-digit permutations. I'd probably convert the generated (winning) number to a string, and also the user's number. You can then step through the winning number, counting the number of digits from the winning number that match unused digits from the user's number. With the count, you know what, if anything, the user won. Note that when a digit has been matched, you need to delete it, so that when the winning number is 666 and the user enters 456, you don't count three matches with the 6.

Like this:

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

int main(void)
{
    int win;
    int num;

    srand(time(NULL));
    win = rand() % (999 - 100 + 1) + 100;

    printf("Here is the winning number: %d\n", win);

    printf("Enter three digit number to win lottery:\n");
    if (scanf("%d", &num) != 1)
        return 1;

    if (num == win)
        printf("For exact match you get $100,000\n");
    else if (num < 0 || num > 999)
        printf("Your number is out of range - you win nothing\n");
    else
    {
        char win_str[4];
        char try_str[4];
        sprintf(win_str, "%d", win);
        sprintf(try_str, "%d", num);
        int match = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (win_str[i] == try_str[j])
                {
                    try_str[j] = 'x';
                    match++;
                    break;
                }
            }
        }

        switch (match)
        {
        case 0:
            printf("No digits in %.3d match %3d - you win nothing\n", num, win);
            break;
        case 1:
            printf("One digit of %.3d matches %3d - you win $10,000\n", num, win);
            break;
        case 2:
            printf("Two digits of %.3d match %3d - you win $20,000\n", num, win);
            break;
        case 3:
            printf("Three digits of %.3d match %3d - you win $50,000\n", num, win);
            break;
        default:
            printf("The impossible happened (%.3d vs %3d gives %d matches)\n", num, win, match);
            break;
        }
    }
    return 0;
}

다른 팁

For some reason, you have declared num[0] which makes num an array of zero length.

Note that the "winning number" is not really a number but a digit string.

You should declare num to be a character array:

char num[4];

You can read this with:

 scanf("%3s", num)

You then need to convert the ASCII characters read as num[0], num[1], num[2] so that you can compare them with the digits of the random number.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top