#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Absolute beginner needs help


    I'm currently doing an assignment for college, the assignment is you have to enter students names and whether they pass or fail. The way mine works is you just press 1 for a pass and 2 for a fail, although it works, I dont like it.. Could anyone help me write the code for it so if i enter a number less than 40 it prints Fail and a number 40 or more it prints Pass

    Code:
    /* Name: C Assignment
     Author: Robert Leniston
     Date: 13/12/12 */
     #include <stdio.h>
     int main ()
     { 
    
    int i, grade, pass = 0, fail = 0, count = 0, sum = 0 ;
     char str[20]; 
    printf("Roberts Assignment\tWelcome to my C assignment, it was coded by Robert Leniston\n") ;
    printf("\nEnter the first name of the student then enter it\tThen enter the second name of the student and enter it\n") ;
     /*this gives instructions on what the person will do when the open the project up*/ 
    printf("Enter the Result 1 for a Pass and 2 for a Fail\n") ; printf("\nName \t Grade \t Pass \t Fail ") ; 
    for( i = 1; i < 16; i++ ) 
    
    { FILE *file_ptr ; file_ptr = fopen( "data.txt", "w") ;
    if( file_ptr != NULL ) printf("\n\nName: ") ;
    
    scanf("%s%s", str) ; printf("Enter The Grade: ") ;/*allows you to enter the grade*/ scanf("%d", &grade) ;
    
    if (grade < 1) printf("incorrect Number was Entered") ;
    /*this shows when an incorrect number has been entered*/ 
    
    if (grade >2) printf("incorrect Number was Entered") ; /*this shows when an incorrect number has been entered*/ 
    
    if (grade == 1) printf("\t\tResult: Pass ", ++pass) ;
    if (grade == 2) printf("\t\tResult: Fail ", ++fail) ; 
    sum = pass + fail ; 
    count = 15 - sum ;
     
    } printf("\n\tStudents That Passed : %d" , pass) ;
    /*prints a pass*/ 
    
    printf("\tStudents That Fail : %d\n" , fail) ;/* prints a fail*/ 
    
    printf("\n\tAmmount Of Students Entered :%d" , sum) ; printf("\tAmmount Of Students Missing :%d\n" , count) ; 
    if (pass >= 10) printf("\n\t Bonus To The Instructor!") ; /*prints bonus to the instructor if <10 students pass*/
    
     if (pass < 10) printf("\n\t Instructor Recieves No Bonus!") ; /*prints no bonus if >10 students fail*/ return 0 ; }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    http://forums.devshed.com/c-programm...-c-934789.html
    You managed to post your code properly last time.

    > Sorry the code came out like that, i dont know how to fix it
    Try harder.

    Consider it just a little challenge.

    After all, if you can't master posting code on a forum, it doesn't bode well for learning all the intricacies of programming in general.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Fixed, i think..
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    > Could anyone help me write the code for it so if i enter a number less than 40 it prints Fail and a number 40 or more it prints Pass
    Just how did you manage to write this code?

    I mean, how are you going to cope if you can't change
    Code:
        if (grade == 1)
          printf("\t\tResult: Pass ", ++pass);
    into
    Code:
        if (grade >= 40)
          printf("\t\tResult: Pass ", ++pass);
    Which reminds me, that particular printf is the cause of one of these warnings.
    Code:
    $ gcc -W -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:21:5: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat]
    foo.c:33:7: warning: too many arguments for format [-Wformat-extra-args]
    foo.c:35:7: warning: too many arguments for format [-Wformat-extra-args]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo