Pergunta

Hi i know that there is already a question for the random generator within range, but I don't understand it. I am a beginner in C, and I only know java. In this program I am trying to create a math tutor in C. The program will randomly generate two numbers from 1 to 10, and an operator. It runs, but it does not show the next line, and it keeps showing incorrect answer. Also, why does VS2010 is saying that getch() is undefined? Here is the code:

 int ans;
 int ans1;
 int num1 = rand() % 10 + 2;
 int num2 = rand() % 10;
 int operation = rand() % 4;

    printf("\tMATH TUTOR\n");
    if(operation == 1){
        printf("What is %d + %d ?", num1, operation, num2);
        scanf_s("%d",ans1);
        ans = num1 + num2;
        if(ans != ans1){
            printf("Incorrect! Try Again!");
            do{
                scanf_s("%d", &ans1);
            }while( ans != ans);
        }else{
            printf("Correct!");
        }
        }else if(operation == 2){
            printf("What is %d - %d ?", num1, operation, num2);
            scanf_s("%d",&ans1);
            ans = num1 - num2;
            if(ans != ans1){
                printf("Incorrect! Try Again!");
                do{
                    scanf_s("%d", &ans1);
                }while( ans != ans);
            }else{
                printf("Correct!");
                }
        }else if(operation == 3 ){
            printf("What is %d * %d ?", num1, operation, num2);
            scanf_s("%d",&ans1);
            ans = num1 * num2;
            if(ans != ans1){
                printf("Incorrect! Try Again!");
                do{
                    scanf_s("%d", &ans1);
                }while( ans != ans);
            }else{
                printf("Correct!");
            }
            }else if(operation == 4){
                printf("What is %d / %d ?", num1, operation, num2);
                scanf_s("%d",&ans1);
                ans = num1 / num2;
                if(ans != ans1){
                    printf("Incorrect! Try Again!");
                    do{
                        scanf_s("%d", &ans1);
                    }while( ans != ans);
                }else{
                    printf("Correct!");
                }
            }

    getch();
    return 0;
}
Foi útil?

Solução 2

There are multiple issues with your code that will probably make it operate differently to how you expect.

You test for operation values of 1 through 4. operation is assigned it's value from rand() % 4. This means that operation will only ever have values of 0 through 3.

Your do while loops all have the same flaw. They test for ans != ans whereas you should be testing for ans != ans1.

Fix these problems and you will get a bit further.

EDIT to give you a better hint

if(operation == 1){
    printf("What is %d + %d ?", num1, num2);
    scanf_s("%d",ans1);
    ans = num1 + num2;
    if(ans != ans1){
        do{
            printf("Incorrect! Try Again!");
            scanf_s("%d", &ans1);
        }while( ans != ans1);
    }
    printf("Correct!");
}

Edit to show use of srand

int ans;
int ans1;
srand((unsigned int)time(NULL));  //I've included your (unsigned int) cast.
int num1 = rand() % 10 + 2;
int num2 = rand() % 10;
int operation = rand() % 4;

Outras dicas

Adding to John Sheridan's: getch() is a non-standard extension to C that was added by many MS-DOS compilers. It was usually defined in <conio.h>. I don't know if VS2010 supports that by default.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top