Results 1 to 3 of 3

Thread: Problem with C coding

  1. #1
    Join Date
    Aug 2008
    Posts
    475

    Problem with C coding

    Hello everyone.

    I am learning C and am stucked somewhere in the code. Basically I want to assign the marks to the grade. However my output comes out to be totally different from what I want. Here is my code:
    Code:
    # include<stdio.h>  
    int main ()  
    {  
        int total = 0, marks = 0, count = 1;  
        float avg = 0;  
        printf( "Enter the marks obtained or -1 to quit:\n");  
        scanf( "%d", &marks );  
        while ( marks != -1 )  
        {  
            total = total + marks;  
            count ++;  
            printf( "Enter the marks obtained or -1 to quit:\n");  
            scanf( "%d", &marks );  
        }  
        if( count != 0 )  
        {  
           avg = (float) total/count;  
           printf( "Marks in grades are:  %.2f ", avg);  
         }  
         else if ( avg >= 80.00 )  
               printf( "Marks is in grades 'A'.\n");  
         else if( avg < 80.00 && avg >= 70.00 )  
               printf( "Marks is in grades 'B'. \n");  
         else if( avg < 70.00 && avg >= 60.00 )  
               printf( "Marks is in grades 'C'. \n");  
         else if( avg < 60.00 && avg >= 40.00 )  
               printf( "Marks is in grades 'D'.\n");  
         else  
               printf( "\nMarks is in grades 'F'. \n");  
               printf( "You enter: %d times\n", count);  
               return 0;  
    }
    Any comments

  2. #2
    Join Date
    May 2008
    Posts
    188
    Basically the problem is that you are calculating avg value in the if statement and testing it in the else if portion.

    If the if statement is true then the compiler will execute it and the other statements (else if or else) will be skipped. On the contrary, if the if statement is false then the Else if statement is executed.
    So keep this in mind and modify your code accordingly.

  3. #3
    Join Date
    Aug 2008
    Posts
    475
    Thanks unlimitedtech for your help. I modified my code as you suggested me and got the result.

    Thanks once again.

Similar Threads

  1. java coding
    By unikshegz in forum Software Development
    Replies: 1
    Last Post: 27-10-2010, 01:56 AM
  2. How to use the pop_heap() in C++ coding
    By Garrick in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 05:28 PM
  3. Help in coding documents
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 02:34 AM
  4. C++ coding faster?
    By Chandrakant81 in forum Software Development
    Replies: 3
    Last Post: 17-02-2009, 10:51 PM
  5. Problem with a table while coding on PHP
    By Jacek in forum Software Development
    Replies: 4
    Last Post: 12-11-2008, 02:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,892,149.97062 seconds with 17 queries