Thread: Super Simple C

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    1

    Super Simple C

    So, im very new to C and I'm trying to teach myself the language.
    My buddy has been giving me these little programs to make and for the life of me I cannot get this to work. All I have to do is write a code that will ask for the user to input a grade of A B C D or E, of the user says A, B, or C then print pass, if the user says D or E print fail. Set the code to ask for the a grade 10 times. The code i have is:
    Code:
    #include<stdio.h>
    int main ()
    {
        int i=0;
        for(i=0;i<10;i++)
    {
    
    
        char c;
        printf("Please enter grade\n");
        scanf("%c", &c);
            if(c == 'A' || c=='B' || c=='C') printf("Pass\n");
            if(c=='D' || c=='E') printf("Fail\n");
    
    
    
    
    }
    return 0;
    }
    It compiles fine, but when i run the program i get:
    Please enter grade
    A
    Pass
    Please enter grade
    Please enter grade
    B
    Pass
    Please enter grade
    Please enter grade
    C
    Pass
    Please enter grade
    Please enter grade
    D
    Fail
    Please enter grade
    Please enter grade
    E
    Fail
    Please enter grade

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, "%c" is special for scanf, it's a request that doesn't ignore whitespace( newlines, spaces, tabs etc ). That extra printing is there because the input stream really is "A\nB\nC\nD\nE\n"( '\n' is an escape sequence that means newline )

    If you do want to ignore whitespace, which you probably do, then you want to add a space before the %c, inside the string of course.
    Devoted my life to programming...

  3. #3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Super simple - Opening/Reading a file; Don't get it
    By Bakenshake in forum C Programming
    Replies: 5
    Last Post: 11-17-2013, 07:18 PM
  2. Best practice for super simple program
    By samwillc in forum C++ Programming
    Replies: 10
    Last Post: 06-02-2012, 04:29 PM
  3. Super Quick Help
    By Black_Epiphany in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2011, 11:45 AM
  4. Super Tic Tac Toe
    By mcdant01 in forum C++ Programming
    Replies: 10
    Last Post: 09-15-2009, 05:15 PM
  5. Super in need of Help
    By Psyche in forum C Programming
    Replies: 2
    Last Post: 07-10-2008, 10:42 AM

Tags for this Thread