for/while循环在C中的内存清除



在我的程序中,它只是询问编写了多少个测试,然后返回平均值。但是,我对它进行了一些修改,以便它询问输入的标记是否正确。

问题1:它不允许您输入所有测试的分数
问题2:如果标记是错误的,它重新开始,但保留以前的输入在它的记忆?我怎么修理?

代码如下:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    //int variables for grade
    unsigned int counter; //number of grades to be entered next
    int grade;
    int total;
    float average;
    // user input
    int userInput; // amount of tests
    int yesNo; 
    //amount of test passed
    unsigned int pass = 0;
    unsigned int fail = 0;
    int doCount = 1;
    //unsigned int test;
//---------------------------------------------------------------------------------------------------// 
    //standards for program to abide to
    total = 0; //Total amount of test to be set to zero, until while statement
    counter = 1; //Loop counter to start from one
//---------------------------------------------------------------------------------------------------// 
    printf ("Please enter amount of test you've written so far: ");
    scanf ("%d", &userInput);
    //printf ("%d", userInput);
//---------------------------------------------------------------------------------------------------//
    do {
        //Body of calculations of program
        for(counter = 0; counter <= userInput; ++counter) { //for loop that correlates to userInput for amount of passes and test marks
            printf ("Please enter percentage mark: "); //prompt for test mark
            scanf("%d", &grade);
            total = total + grade;
            counter = counter + 1;
            if (grade >= 40) {  //if statement for pass or fail
               pass = pass + 1;
            } else {
                 fail = fail + 1;
                }
        }//end of for loop
        printf ("Are the grades entered correct? (1 = yes, 2 = no): "); // user input for yesNo - are inputs correct    
        scanf ("%d", &yesNo);
        if (yesNo == 2) {
         } else {
            average = ((float)total / userInput); //Getting average for tests so far
                //if statement to clarify if you're passing
            if (average < 40) {
                printf ("nYou are below sub minimum!n");
                printf ("Your overall average is: %.2f %n", average);
                printf ("Passed: %dn", pass);
                printf ("Failed: %d", fail);
            } else if (average >= 75){
                printf ("nYou have a distinction agregate!n");
                printf ("Your overall average is: %.2f %n", average);
                printf ("Passed: %dn", pass);
                printf ("Failed: %d", fail);
            } else {
                printf ("nYour overall average is: %.2f %n", average);
                printf ("Passed: %dn", pass);
                printf ("Failed: %d", fail);
            }
        doCount = 2;    
        }
    } while (doCount == 1);
    average = ((float)total / userInput); //Getting average for tests so far
//---------------------------------------------------------------------------------------------------//     
    getch ();
    return 0;
}

在你的do while循环中,当你进行第二次循环时,你需要重置变量。具体来说,应该将total变量重置为零。你在do while循环之外第一次执行它但是一旦它在循环中进行第二次循环它就不会被重置为0。至于不读取所有测试输入,如果它要求9,但你需要10,那么很可能是for循环的问题。我通常使用计数器++而不是++counter,因为它在操作之后而不是在操作之前增加计数器。这可能是也可能不是我没有运行你的代码的原因,但它值得一看。

我已经编辑了您的代码并注释了更改:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    //int variables for grade
    unsigned int counter; //number of grades to be entered next
    int grade;
    int total;
    float average;
    // user input
    int userInput; // amount of tests
    int yesNo;
    //amount of test passed
    unsigned int pass = 0;
    unsigned int fail = 0;
    int doCount = 1;
    //unsigned int test;
    //---------------------------------------------------------------------------------------------------// 
    //standards for program to abide to
    total = 0; //Total amount of test to be set to zero, until while statement
    counter = 0; //Loop counter to start from zero, It's always better to start from zero
                 //---------------------------------------------------------------------------------------------------// 
    printf("Please enter amount of test you've written so far: ");
    scanf("%d", &userInput);
    //printf ("%d", userInput);
    //---------------------------------------------------------------------------------------------------//
    do {
        //Body of calculations of program
        total = 0; //You need to reset total pass and fail
        pass = 0;
        fail = 0;
        for (counter = 0; counter < userInput; ++counter) { //for loop that correlates to userInput for amount of passes and test marks
            printf("Please enter percentage mark: "); //prompt for test mark
            scanf("%d", &grade);
            total = total + grade;
            //counter = counter + 1; You DON't need that
            if (grade >= 40) {  //if statement for pass or fail
                pass = pass + 1;
            }
            else {
                fail = fail + 1;
            }
        }//end of for loop
        printf("Are the grades entered correct? (1 = yes, 2 = no): "); // user input for yesNo - are inputs correct    
        scanf("%d", &yesNo);
        if (yesNo == 2) {
        }
        else {
            average = ((float)total / userInput); //Getting average for tests so far
                                                  //if statement to clarify if you're passing
            if (average < 40) {
                printf("nYou are below sub minimum!n");
                printf("Your overall average is: %.2f %n", average);
                printf("Passed: %dn", pass);
                printf("Failed: %d", fail);
            }
            else if (average >= 75) {
                printf("nYou have a distinction agregate!n");
                printf("Your overall average is: %.2f %n", average);
                printf("Passed: %dn", pass);
                printf("Failed: %d", fail);
            }
            else {
                printf("nYour overall average is: %.2f %n", average);
                printf("Passed: %dn", pass);
                printf("Failed: %d", fail);
            }
            doCount = 2;
        }
    } while (doCount == 1);
    average = ((float)total / userInput); //Getting average for tests so far
                                          //---------------------------------------------------------------------------------------------------//     
    getch();
    return 0;
}

最新更新