Thread: The letters

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    The letters

    O.k dudes I've tried just about everything to get this crap to work anyways. I just got through failing a test this week that basically asked us to enter a numerical value and return a letter grade using user written function . I $$$$ing hate character values. I never can seem to get them to return a letter. I got this instead
    enter a grade
    98
    the grade you entered is ♦
    enter a grade

    here is my code
    can someone please help me figure this out why is it not giving me a letter value and a box instead. here is the code

    #include <stdio.h>
    char getletgrade(char);
    int main()
    {
    int d;
    char total;
    do

    {
    printf("enter a grade\n");
    scanf("%d", &d);

    printf("the grade you entered is %c\n", total);

    }
    while (total !=999);
    return 0;
    }
    char getletgrade(char c)
    {
    char A, B, C, D;
    int grade;
    if
    (grade > 89)
    c = 'A';
    else if
    (grade > 80 && grade < 89)
    c = 'B';
    else if
    (grade > 70 && grade < 79)
    c = 'C';
    else if
    (grade > 60 && grade < 69)
    c = 'D';
    else if
    (grade < 60)
    c = 'F';
    return (c);
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    First of all, read http://cboard.cprogramming.com/showt...threadid=25765

    1. After that you have to actually call getletgrade() to give you back a value.

    Something like:
    Code:
    printf("the grade you entered is %c\n", getletgrade(d));
    2.You delcared "total" and never assign it a value, and you use it as a sentinel at the loop "while (total !=999);" which will have undefine behaviour since you did not initialise it's value as well.

    You should check against "d" instead, that is where your user input variable is stored.

    3. you should declare you protoype
    Code:
    char getletgrade(int);
    since you're interest in comparing an integer value.
    Merc

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    WOOPS.

    My bad I was a bit hasty in my cutting and pasting here the actual code. The code is working now but all it gives me back no matter what number I enter is an F . WIERD!!!!!
    #include <stdio.h>
    char getletgrade(char);
    int main()
    {
    int d;
    char c;
    do

    {
    printf("enter a grade\n");
    scanf("%d", &d);

    printf("the grade you entered is %c\n", getletgrade(c));

    }
    while (d !=999);
    return 0;
    }
    char getletgrade(char c)
    {
    char A, B, C, D;
    int grade;
    if
    (grade > 89)
    c = 'A';
    else if
    (grade > 80 && grade < 89)
    c = 'B';
    else if
    (grade > 70 && grade < 79)
    c = 'C';
    else if
    (grade > 60 && grade < 69)
    c = 'D';
    else if
    (grade < 60)
    c = 'F';
    return (c);
    }
    Last edited by drdodirty2002; 04-25-2003 at 10:16 PM.

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I guess you want to scan an int and return its letter?

    Code:
    #include <stdio.h>
    
    char getletgrade(int);
    
    int main()
    {
       int d;
    		
       for(; ;)
       {
           printf("enter a grade or 999 to quit\n");
           scanf("%d%*c", &d);
    		
           if(d == 999) break;
           
           printf("the grade you entered is %c\n", getletgrade(d)); 	
        }
    	
         return 0;
    }
    
    char getletgrade(int in)
    {
        char letter;
    	
        if(in >= 90)
            letter = 'A';
        else if(in >= 80)
            letter = 'B';
        else if(in >= 70)
            letter = 'C';
        else if(in >= 60)
            letter = 'D';
        else
            letter = 'F';
    	    	
        return letter;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>int grade;
    >>if (grade > 89)
    You didn't give grade a value before checking it. Like ronin showed, you're presumably supposed to be passing that value in to the function.

    Read this for better methods of getting a number from the user.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. need to count individual letters in input
    By epox in forum C Programming
    Replies: 12
    Last Post: 05-22-2006, 06:32 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. drive letters
    By metsman20 in forum Tech Board
    Replies: 4
    Last Post: 12-11-2003, 12:54 PM
  5. Switching letters
    By XiReDDeViLiX in forum Windows Programming
    Replies: 4
    Last Post: 06-06-2002, 06:48 AM