dimanche 9 août 2015

for/while loop memory clearing in C

in my program I'm messing around with, it simply asks for how many tests one has written and then returns an average. However I've modified it a bit so that it asks if the marks entered are correct. Problem 1: It doesn't let you input your marks for all your tests Problem 2: If the marks are wrong it starts over but keep the previous inputs in it's memory? How do I fix the? here's the code... #include #include

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

Aucun commentaire:

Enregistrer un commentaire