Maria Khalid
Maria Khalid

Reputation: 189

C loop skipping an iteration over input

It is a very simple program, for calculating the cgpa. But loop is skipping an iteration over input and call default for every that skipped iteration.

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

int main(){

    float A=4.0, b=3.50, B=3.0, c=2.50,C=2.0;
    float cgpa=0;
    char grade;

for (int i=0; i<5;i++){

        printf("\nEnter grade of your subject:\n");
        grade = getchar( );

        switch(grade){

            case 'A':
            cgpa=cgpa+A;
            break;
            case 'b':
            cgpa=cgpa+b;
            break;
            case 'B':
            cgpa=cgpa+B;
            break;
            case 'c':
            cgpa=cgpa+c;
            break;
            case 'C':
            cgpa=cgpa+C;
            break;
            default:
            printf("\nSorry you have entered a wrong value please try again\n");
        }}
    printf("\n Your cgpa is:%f", cgpa/5);

return 0;   
    }

Upvotes: 3

Views: 1924

Answers (3)

Cherubim
Cherubim

Reputation: 5457

When you enter a character and hit enter, character gets accepted by getchar() and when the loop starts again \n is accepted because of the enter key you have given in the previous iteration, so.... Everytime you enter a character and hit enter... two inputs are taken and thus, this behaviour is observed. To overcome this, use

   scanf(" %c",&grade);

Note : Here a space is given in front of %c to omit empty spaces

Instead of:

    grade=getchar()

In the loop.

Upvotes: 1

user3629249
user3629249

Reputation: 16540

the following code compiles cleanly, and properly performs the desired algorithm and handles white space, like newlines and invalid grade values.

Notice the ease of readability when appropriate indenting and code block separation are implemented

#include <stdio.h> // printf(), getchar()
#include <ctype.h> // toupper()

// MAX_GRADES makes it easy to adjust number of 'grade' inputs
#define MAX_GRADES (5)

int main()
{

    // list variables one per line
    // and it would be better if these were #defines
    // instead of taking up stack space
    float A=4.0f;
    float b=3.50f;
    float B=3.0f;
    float c=2.50f;
    float C=2.0f;

    float cgpa=0.0f;
    int grade;  // << getchar() returns an integer not a character

    for (int i=0; i<MAX_GRADES; i++)
    {

        printf("\nEnter grade of your subject:\n");
        grade = getchar( );

        if( 'A' <= toupper(grade) && 'Z' >= toupper(grade) )
        {
            switch(grade)
            {

                case 'A':
                    cgpa=cgpa+A;
                    break;

                case 'b':
                    cgpa=cgpa+b;
                    break;

                case 'B':
                    cgpa=cgpa+B;
                    break;

                case 'c':
                    cgpa=cgpa+c;
                    break;

                case 'C':
                    cgpa=cgpa+C;
                    break;

                default:
                    printf("\nSorry you have entered a wrong value please try again\n");
                    i--; // adjust to not count bad input
                    break;
            } // end switch
        }

        else
        {
            i--; // adjust to not count newline, non-alpha inputs, etc
        }
    } // end for

    printf("\n Your cgpa is:%f", cgpa/MAX_GRADES);

    return 0;
} // end function: main

Upvotes: 1

arc_lupus
arc_lupus

Reputation: 4114

When you are calling getchar(), you are getting two chars from the command line (f.ex. A\n), but you are only fetching one at a time. Thus you get the \n the second time you are looping. Fix? Do getchar() again after your switch-case while omitting the result.

Furthermore, getchar() returns an int-value, but you are making a char out of it, which can lead to strange behaviour.

Upvotes: 2

Related Questions