3

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;   
    }
arc_lupus
  • 3,942
  • 5
  • 45
  • 81
Maria Khalid
  • 189
  • 2
  • 15
  • No Ds or Fs? An optimist, I see. – R_Kapp Mar 02 '16 at 18:54
  • `grade` should be an `int`, since `getchar()` is returning one. But there might be more problems within your code. – pzaenger Mar 02 '16 at 18:55
  • 1
    It is probably consuming the newline character. There are hundreds of related questions. – Eugene Sh. Mar 02 '16 at 18:57
  • 1
    `getchar()` reads all characters including spaces and newlines. If you enter your grades line by line or with spaces in between, those will be treated like grades. Treat them specially. – M Oehm Mar 02 '16 at 18:59
  • 1
    `printf("\nSorry you have entered a wrong value please try again\n");` It would be informative for the user (and the programmer) of the program to actually **show** the offending character. – wildplasser Mar 02 '16 at 19:00
  • Please consistently indent the code. suggest 4 spaces after every opening brace '{' and unindent before every closing brace '}'. Do not use tabs for indenting as every word processor/editor has the tab stops/tab width set differently. – user3629249 Mar 02 '16 at 19:02
  • float literals always have a `.` and a trailing `f`. Without out the `.` the value is an `integer`. Without the trailing `f`, the value is a `double` – user3629249 Mar 02 '16 at 19:07
  • Possible duplicate of [getchar() and scanf() skipped in C](http://stackoverflow.com/questions/28308319/getchar-and-scanf-skipped-in-c) – Cloud Mar 02 '16 at 19:12
  • if the user enters the grades as: `aaaaa` then the code will work. However if the user enters the grades, on separate lines, separated by newline sequences or enters the grades separated by a space or tab, then the code will fail. the `switch()` statement needs to be inside its' own loop so a incorrect entry will not reduce the number of grades entered. Also suggest the `default` case have a call to `getchar()` to get the next char from stdin. The current problem is the newline (and similar trash) is not being consumed – user3629249 Mar 02 '16 at 19:12
  • (cont) a newline is NOT a `wrong value`, Rather it will be in the stream stdin for every grade entered by the user – user3629249 Mar 02 '16 at 19:16

3 Answers3

2

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.

arc_lupus
  • 3,942
  • 5
  • 45
  • 81
1

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.

Cherubim
  • 5,287
  • 3
  • 20
  • 37
1

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
user3629249
  • 16,402
  • 1
  • 16
  • 17