for / while循环内存清除C

时间:2015-08-09 14:42:35

标签: c if-statement for-loop while-loop

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

问题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: %d\n", 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: %d\n", pass);
                printf ("Failed: %d", fail);
            } else {
                printf ("\nYour overall average is: %.2f %\n", average);
                printf ("Passed: %d\n", pass);
                printf ("Failed: %d", fail);
            }
        doCount = 2;    
        }
    } while (doCount == 1);

    average = ((float)total / userInput); //Getting average for tests so far
//---------------------------------------------------------------------------------------------------//     

    getch ();
    return 0;
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我已编辑您的代码并对更改进行了评论:

#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: %d\n", 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: %d\n", pass);
                printf("Failed: %d", fail);
            }
            else {
                printf("\nYour overall average is: %.2f %\n", average);
                printf("Passed: %d\n", pass);
                printf("Failed: %d", fail);
            }
            doCount = 2;
        }
    } while (doCount == 1);

    average = ((float)total / userInput); //Getting average for tests so far
                                          //---------------------------------------------------------------------------------------------------//     

    getch();
    return 0;
}