Do while loops, help

+3 Jacky L · August 27, 2014
So I just made an account to ask this question. I was watching tutorial 26 of C and did my code a bit differently than in the video. When I run this, everything works fine until I enter the number of tests in which after that it just spams "Enter number of tests: " Why is it not going through?




#include <stdio.h>
#include <stdlib.h>

int main()
{

    float test = 0;
    float numberoftests;
    float grade;
    float average = 0;
    printf("Enter number of tests: ");
    scanf(" %.0f", &numberoftests);
    do{
        printf("Enter test grades: ");
        scanf(" %.1f", &grade);
        average += grade;
        test++;
    }while(test != numberoftests);
    average /= numberoftests;
    printf("Average grades: %.1f from %.0f tests.\n", average, numberoftests);

    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
+2 Горан Куга · October 14, 2014
Hi buddy so i fixed your code. Check comments in code. Also after reading the coments for first code i did a same one but with using ints.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float test = 0, average = 0, numberOfTests, grade; // everything was fine with your floats i just put it like this to make code looks nicer.

    printf("Enter number of tests: ");
    scanf(" %f", &numberOfTests); // when using scanf you can only say %f for float you can't say %.0f if u want to make sure user enters integer then you don't use float instead use int but when you use int instead of saying %f you need to say %d.

    do{
        printf("Enter test grades: ");
        scanf(" %f", &grade); // here same goes as for numberOfTests if u want to use integer then use int instead of floats.
        average += grade;
        test++;
    }while(test != numberOfTests);

   average /= numberOfTests;
    printf("Average grades: %.2f from %.0f tests.\n", average, numberOfTests); // here i just put %.2f instead of yours %.1f to make it show two digits. If u were using ints you would put here for tests %d.
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int test = 0, numberOfTests, grade; // ints for test, numberOfTests and grade.
    float average; // we use float for average because average may not be integer.

    printf("Enter number of tests: ");
    scanf(" %d", &numberOfTests); // because our numberOfTests is int we use here %d instead of %f.

    do{
        printf("Enter test grade: ");
        scanf(" %d", &grade); // we use %d instead of %f because our grade is int not float.
        average += grade;
        test++;
    }while(test != numberOfTests);

    average /= numberOfTests;
    printf("Average grades: %.2f from %d tests.", average, numberOfTests); // notice that here we use %.2f to show up to two digits and %d for numberOfTests because numberOfTests is int.

    return 0;
}

I want to share my own version of calculating average grades and i also provide same code as below but with comments  http://pastebin.com/YkNaE5YL.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

/*
Average grade calculator by Goran
*/

int main()
{
    int loop = 0, numberOfTests, grade;
    float average;

    printf("Enter number of tests: ");
    scanf(" %d", &numberOfTests);

    while(numberOfTests <= 0){
          printf("You need to enter correct number of tests: ");
          scanf(" %d", &numberOfTests);
    }

printf("\n");

    do{
        printf("Enter test grade: ");
        scanf(" %d", &grade);
        average += grade;
        loop++;
    }while(loop != numberOfTests);

    average /= numberOfTests;

    if(numberOfTests > 1){
       printf("Average grade from %d tests is %.2f \n \n", numberOfTests, average);
    }else{
       printf("Average grade from 1 test is %.2f \n \n", average);
    }

    system("pause");
    return 0;
}
0 Mr belbala · September 5, 2014
the probleme is in the scanf(" %.0f", &numberoftests);
you should right it like that 
scanf(" %f", &numberoftests);
and if you want 2 float afte the sem colone type it in the printf() function like this
printf("the results is %.2f", numberoftests);
+2 Mattheus Lee · August 28, 2014
I believe it has something to do with your number of tests being a float value instead of an integer.
I tried running your code and it also returned me an infinite loop of text.

I made a few modifications which seems to make the code work better, have a look.
#include <stdio.h>
#include <stdlib.h>

int main()
{

    int test = 0; //changed variable type
    int numberoftests; //changed variable type
    float grade;
    float average = 0;
    printf("Enter number of tests: ");
    scanf("%d", &numberoftests);
    do
    {
        printf("Enter test grades: ");
        scanf("%f", &grade); // removed the .1
        average += grade;
        test++;
    }while(test != numberoftests);
    average /= numberoftests;
    printf("Average grades: %.1f from %d tests.\n", average, numberoftests);

return 0;
}
  • 1

C

107,198 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator