【问题标题】:for/while loop memory clearing in CC中的for/while循环内存清除
【发布时间】:2015-11-01 12:46:29
【问题描述】:

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

问题 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;
}

【问题讨论】:

  • 请注意,您的循环 for(counter = 0; counter &lt;= userInput; ++counter) 是可疑的。通常,您要么使用for (counter = 0; counter &lt; maximum; ++counter),要么偶尔使用for (counter = 1; counter &lt;= maximum; ++counter)。您的循环迭代userInput+1 次。 (想一想;用户输入 3 为 userInput;循环将 counter 设置为 0、1、2、3。这比用户说的数字多一个循环。)请记住,数组在 C 中从 0 开始索引;这就是为什么在 C 中最常使用第一个替代方案的原因。
  • 您的代码无法干净地编译。如发布的那样,它会导致编译器输出 7 个警告。强烈建议在编译时始终启用所有警告。然后修复警告。除此之外,'getch()' 没有原型,它不是一个可移植的系统函数。建议改用“getchar()”。
  • 这一行:'if (average = 75){'将浮点数与整数进行比较。建议使用:'if (average = 75.f){'
  • 调用系统函数时:'scanf()'总是检查返回值以确保操作成功。
  • 这一行:'printf ("Failed: %d", fail);'不会立即输出/显示建议更改为:'printf ("Failed: %d\n", fail);'请注意格式字符串末尾的附加“\n”。

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


【解决方案1】:

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

【讨论】:

  • 也正如 Viktor 在下面指出的那样 counter = counter + 1 是不需要的。原因是 for 循环会为您完成,即 counter++。因此,您可能会体验到跳过所有其他测试的影响。祝你好运。
【解决方案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 = 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;
}

【讨论】:

  • 谢谢大家!非常感谢
猜你喜欢
  • 2019-11-10
  • 2018-11-05
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 2010-10-07
  • 2014-03-22
  • 2021-06-29
  • 2015-10-21
相关资源
最近更新 更多