算术测验在减法和除法循环崩溃时崩溃并且进程返回-1073741819

时间:2012-04-10 13:18:09

标签: c

其他一切正常但是尝试进入减法或除法部分(练习的一部分不是在减法中提出具有否定答案的问题,或者除以0或答案小于1的除法问题)即可。起初它有效,然后尝试回答另一个问题。它给出了我的初始化函数中未包含的这个复杂问题所以我尝试回答它,它仍然可以检测到对错的答案。然后它给出了这个32 - 9或32/9(无法回答除法部分,因为它需要一个余数并且问题太复杂了)。在你给出正确的答案之后尝试回答另一个减法/除法问题并且它会崩溃并将此错误输出处理返回-1073741819 我的代码有什么问题?也提前谢谢! BTW我对我的代码中应该限制除法和减法的部分发表评论

/* Arithmetic Quiz Practice Program */
#include <stdio.h>
#include <stdlib.h>
int numbers[10];
int clear(void);
int initialize(void);
int additionquiz(void);
int subtractionquiz(void);
int multiplicationquiz(void);
int divisionquiz(void);

/* Main Menu */
int main()
{
    while(1==1)
    {
        int choice;
        initialize();
        printf("Arithmetic Quiz 4/10/2012");
        printf("\n1 - Addition Quiz\n2 - Subtraction Quiz\n3 - Multiplication Quiz\n4 - Division Quiz\n5 - Exit Program\n");
        scanf("%d",&choice);
        if(choice==1)
        {
            clear();
            additionquiz();
        }
        else if(choice==2)
        {
            clear();
            subtractionquiz();
        }
        else if(choice==3)
        {
            clear();
            multiplicationquiz();
        }
        else if(choice==4)
        {
            clear();
            divisionquiz();
        }
        else if(choice==5)
        {
            exit(EXIT_SUCCESS);
        }
        else
        {
            printf("\n%cPlease input a valid option\n",7);
            main();
        }
    }
    return 0;
}
/* Function for clearing the page */
int clear()
{
    int i;
    for(i=0;i<25;i++)
    {
        printf("\n");
    }
    return 0;
}
/* Function for initializing the Array */
int initialize()
{
    numbers[0]=9;
    numbers[1]=5;
    numbers[2]=1;
    numbers[3]=4;
    numbers[4]=7;
    numbers[5]=8;
    numbers[6]=3;
    numbers[7]=6;
    numbers[8]=2;
    numbers[9]=0;
    return 0;
}

/* Function for the Addition Quiz */
int additionquiz()
{
    /* Randomizing the question in addition quiz */
    int a,b,diff,ans,again;
    a=0;
    diff=1;
    b=a+diff;
    if(a>9)
    {
        a=0;
        diff++;
    }
    if(diff>9);
    {
        diff=0;
    }

    if(b>9);
    {
        b=0;
    }

    /* Main part of the addition quiz */
    while(1==1)
    {
        printf("\n%d + %d = ",numbers[a],numbers[b]);
        scanf("%d",&ans);
        if(ans==numbers[a]+numbers[b])
        {
            printf("\nYour answer is CORRECT!!!\n");
            a++;
        }
        else
        {
            printf("\nYour answer is WRONG!!!\n");
            additionquiz();
        }   
        /* The loop for addition quiz" */
        while(1==1)
        {
            printf("\n1 - Answer another addition question\n2 - Go back to main menu\n3 - Exit program\n");
            scanf("%d",&again);
            if(again==1)
            {
                clear();
                break;
            }
            else if(again==2)
            {
                clear();
                main();
            }
            else if(again==3)
            {
                exit(EXIT_SUCCESS);
            }
            else
            {
                printf("%cPlease input a valid option.\n",7);
                continue;
            }
        }
        continue;
    }
}
/* Function for the subtraction quiz */
int subtractionquiz()
{
/* Randomizing the question in subtraction quiz */
    int a,b,diff,ans,again;
    a=0;
    diff=1;
    if(a>9)
    {
        a=0;
        diff++;
    }
    if(diff>9);
    {
        diff=0;
    }
    b=a+diff;
    if(b>9);
    {
        b=0;
    }
/* Main part of the subtraction quiz */
    while(1==1)
    {
    /* Not allowing questions with negative answer */
        while(numbers[a]<numbers[b])
            {
                a++;
            }

        printf("\n%d - %d = ",numbers[a],numbers[b]);
        scanf("%d",&ans);
        if(ans==numbers[a]-numbers[b])
        {
            printf("\nYour answer is CORRECT!!!\n");
            a++;
        }
        else
        {
            printf("\nYour answer is WRONG!!!\n");
            subtractionquiz();
        }
        /* Loop for the subtraction quiz */
        while(1==1)
        {
            printf("\n1 - Answer another subtraction question\n2 - Go back to main menu\n3 - Exit program\n");
            scanf("%d",&again);
            if(again==1)
            {
                clear();
                break;
            }
            else if(again==2)
            {
                clear();
                main();
            }
            else if(again==3)
            {
                exit(EXIT_SUCCESS);
            }
            else
            {
                printf("%cPlease input a valid option.\n",7);
                continue;
            }
        }
        continue;
    }
}
/* Function for multiplication quiz */
int multiplicationquiz()
{
    /* Randomizing the multiplication quiz */
    int a,b,diff,ans,again;
    a=0;
    diff=1;
    b=a+diff;
    if(a>9)
    {
        a=0;
        diff++;
    }
    if(diff>9);
    {
        diff=0;
    }

    if(b>9);
    {
        b=0;
    }
    /* Main part of the multiplication quiz */
    while(1==1)
    {
        printf("\n%d x %d = ",numbers[a],numbers[b]);
        scanf("%d",&ans);
        if(ans==numbers[a]*numbers[b])
        {
            printf("\nYour answer is CORRECT!!!\n");
            a++;
        }
        else
        {
            printf("\nYour answer is WRONG!!!\n");
            clear();
            multiplicationquiz();
        }
        /* Loop for multiplication quiz */
        while(1==1)
        {
            printf("\n1 - Answer another multiplication question\n2 - Go back to main menu\n3 - Exit program\n");
            scanf("%d",&again);
            if(again==1)
            {
                clear();
                break;
            }
            else if(again==2)
            {
                clear();
                main();
            }
            else if(again==3)
            {
                exit(EXIT_SUCCESS);
            }
            else
            {
                printf("%cPlease input a valid option.\n",7);
                continue;
            }
        }
        continue;
    }
}
/* Function for division quiz */
int divisionquiz()
{
    /* Randomizing the division quiz */
    int a,b,diff,ans,again,remain;
    a=0;
    diff=1;
    if(a>9)
    {
        a=0;
        diff++;
    }
    if(diff>9);
    {
        diff=0;
    }
    b=a+diff;
    if(b>9);
    {
        b=0;
    }
    /*Main part of the division quiz */
    while(1==1)
    {
        /* Not allowing division by 0 or answers less than 1 */
        if(numbers[b]==0 || (numbers[a]<numbers[b]))
        {
                a++;
                continue;
        }
        printf("%d %% %d =\n",numbers[a],numbers[b]);
        printf("What is the whole number in your answer?\n");
        scanf("%d",&ans);
        printf("\nWhat is the remainder in your answer?(0 if none)\n");
        scanf("%d",&remain);
        if((ans==numbers[a]/numbers[b])&&(remain==numbers[a]%numbers[b]))
        {
                printf("\nYour answer is CORRECT!!!\n");
                a++;
        }
        else
        {
                printf("\nYour answer is WRONG!!!\n");
                divisionquiz();
        }
        /* Loop for division quiz */        
        while(1==1)
        {
            printf("\n1 - Answer another division question\n2 - Go back to main menu\n3 - Exit program\n");
            scanf("%d",&again);
            if(again==1)
            {
                clear();
                break;
            }
            else if(again==2)
            {
                clear();
                main();
            }
            else if(again==3)
            {
                exit(EXIT_SUCCESS);
            }
            else
            {
                printf("%cPlease input a valid option.\n",7);
                continue;
            }
        }
        continue;
    }
}

3 个答案:

答案 0 :(得分:1)

这是一个问题:

if(diff>9);

if条件后的尾部分号:这种情况发生在几个地方 并且意味着{}之后的if中的任何代码(仅在条件为true时才会执行)将始终执行。

答案 1 :(得分:1)

有几个问题。但是你看到奇怪数字的原因是a最终会超过numbers[]数组的末尾,进入未分配的内存。

由于你开始学习C,这里有一些提示。

initialize(); // Only need to call this once, since the array never changes.
while(1==1)  // This can be "while(1)"
{
    int choice;
    printf("Arithmetic Quiz 4/10/2012");
    // You can improve readability by splitting long literals like this.
    printf("\n1 - Addition Quiz\n2 - Subtraction Quiz\n"
      "3 - Multiplication Quiz\n4 - Division Quiz\n5 - Exit Program\n");
    scanf("%d",&choice);
    // You can simplify a list of "else if" statements with "switch",
    // and you might want to call clear() only once:
    /*
    if(choice==1)
    {
        clear();
        additionquiz();
    }
    else if(choice==2)
    . . .
    */
    clear();  // Always call this.
    switch (choice) {
    case 1:
      additionquiz();
      break;
    case 2:
      subtractionquiz();
      break;
    ...
    default:
      printf("\n%cPlease input a valid option\n",7);
      // main(); Not necessary
      break;
  }

当您不关心函数的返回值时,可以声明它返回void。明确声明一个no-args函数也是一个好主意:

/* Function for clearing the page */
//int clear()
void clear(void)  // Takes no args, returns nothing.
{

在测验的每个部分中,您需要检查<{1}}循环内的ab ,以确保它们始终有效重复相同的测验。

while()

最后,由于您在每个测验中对/* Randomizing the multiplication quiz */ int a,b,diff,ans,again; a=0; diff=1; // vvv THIS BLOCK SHOULD BE INSIDE THE while() LOOP vvv b=a+diff; if(a>9) { a=0; diff++; } if(diff>9); // This semicolon causes the next line to be executed always. { diff=0; } if(b>9); // This semicolon causes the next line to be executed always. { b=0; } // ^^^ THIS BLOCK SHOULD BE INSIDE THE while() LOOP ^^^ /* Main part of the multiplication quiz */ while(1==1) // This can be "while(1)" { a重复相同的验证检查,因此您可能希望将该代码提取到自己的函数中。由于函数可能需要更改值,因此您需要按地址传递它们,然后取消引用指针:

b

答案 2 :(得分:0)

一旦指定“再次”,您将使用递增的 a 来询问下一个减法/除法问题。

问题是您未在 while循环中验证 a 索引。因此,请在 while循环中包含索引验证。