Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 01-09-2008
Member
 
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
Reply With Quote
  #2  
Old 02-09-2008
Member
 
Join Date: May 2008
Posts: 186
Basically the problem is that you are calculating avg value in the if statement and testing it in the else if portion.

Quote:
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.
Reply With Quote
  #3  
Old 02-09-2008
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 04:44 AM.