Thread: Very confused

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Question Very confused

    Ok so I am very new at this and I'm so confused. I have an assignment to convert a numeric grade into a letter grade. Everything works but when I run it no matter what number I enter it returns "F". Clearly either my function is written wrong or I'm calling it wrong (or both). Please can someone help me fix this? Thank you sooooo much.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    char getLetterGrade (int num_grade);
    
    char
    getLetterGrade (void)
    {
    int num_grade;
    
    if (num_grade>=90)
    return 'A';
    else if (num_grade>=80)
    return 'B';
    else if (num_grade>=70)
    return 'C';
    else if (num_grade>=60)
    return 'D';
    else
    return 'F';
    }
    
    int
    main (void)
    {
    char letter_grade;
    int num_grade;
    
    printf ("Enter total grade \t");
    scanf ("%c", &num_grade);
    
    letter_grade=getLetterGrade ();
    
    printf ("The letter grade is %c. \n", letter_grade);
    
    system ("pause");
    return (0);
    }
    Last edited by Prelude; 10-06-2010 at 12:55 PM. Reason: Saw no good reason for OP to delete all content

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    int num_grade;    
                              
         if (num_grade>=90)
    So....what's the value of num_grade when you enter the if/else tree?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    I'm going to be completely honest with you...I have no idea what I'm doing. Because of this I have no idea how to answer your question. All I know is we are supposed to be able to enter a number value for the grade and it is supposed to spit out a letter. Please forgive my complete ignorance.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You're entering a number? You should fix this to be:
    Code:
    scanf ("%d", &num_grade);
    Also, since you expect to access num_grade in your function as well as in main, you should declare it at the top, outside of both.
    Last edited by nonoob; 10-06-2010 at 12:09 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by onefishtwofish View Post
    I'm going to be completely honest with you...I have no idea what I'm doing. Because of this I have no idea how to answer your question. All I know is we are supposed to be able to enter a number value for the grade and it is supposed to spit out a letter. Please forgive my complete ignorance.
    Well, you should maybe ask yourself how the "getlettergrade" function knows what the numerical grade is.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    @nonoob Thank you for the tips.

    @CommonTater I understand that I'm doing something wrong in that it there is no association between the numerical grade and the letter grade...but that's why I'm here. I don't understand what to do to get the function to know what the numerical grade is. I'm sorry that I don't know what I'm doing...but I just don't.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And if you have no idea what you're doing, then go see the person you're paying to TEACH you...the TEACHER!

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The value is not actually being passed to the function.
    This part is right:
    Code:
    char getLetterGrade (int num_grade);
    The other places it's used have something missing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Ok. Thanks. I was under the impression this was a message board designed so people can ask other people who know more than them questions regarding C programming...but apparently it is only reserved for those who know exactly what they're doing. I did in fact ask my teacher but he gave me a similar response and told me to figure it out...which is why I'm here because I couldn't figure it out on my own. I apologize for even posting this and wasting everyone's time. It seems like there is a simple solution to my problem and I was just hoping someone would be able to tell me what I'm doing wrong (and HOW to fix it) so I can be done with this but I guess that isn't going to happen. Thanks to nonoob for actually trying to help.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by onefishtwofish View Post
    @nonoob Thank you for the tips.

    @CommonTater I understand that I'm doing something wrong in that it there is no association between the numerical grade and the letter grade...but that's why I'm here. I don't understand what to do to get the function to know what the numerical grade is. I'm sorry that I don't know what I'm doing...but I just don't.
    It's ok... we've all been there at one time or another.

    The real trick here is to sit down, relax. The real stumpers are almost always something minor. The big picture has already been given ample throught, now it's time to study the minutial...

    How does your number to letter converter *function call* know the number? At what point do you *tell it* what number....

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    @CommonTater Thank you for understanding and being patient and trying to help, it is greatly appreciated. I have to leave to go (not) turn it in so thanks anyways...you at least helped me see where my problem is and what you said was comforting, unlike others who posted making me feel like a complete piece of crap. I hope you have a wonderful day.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Perhaps you didn't read my entire post. I said that num_grade is not accessible in each function. You can define it once at the top of the program outside of any function, or as iMalc suggested to pass it to the function.

    All the help was given for you to fix the problem. You were just in a panic and didn't read carefully. Sorry you didn’t think there was sufficient help.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by onefishtwofish View Post
    @CommonTater Thank you for understanding and being patient and trying to help, it is greatly appreciated. I have to leave to go (not) turn it in so thanks anyways...you at least helped me see where my problem is and what you said was comforting, unlike others who posted making me feel like a complete piece of crap. I hope you have a wonderful day.
    Oh I have my "make you feel like crap" days too.

    The answer is down near the bottom...

    letter_grade=getLetterGrade ();

    should be

    letter_grade=getLetterGrade (num_grade);

    That passes the value into your function so it can evaluate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with following code
    By SeriousTyro in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 10:37 AM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 05:41 PM
  5. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM