Thread: Assigning words to numbers

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    Assigning words to numbers

    Hi, I'm new to C and I want to have a user input a bunch of grades and calculate the gpa.

    I want to have some type of variable whether it's float or int or whatever (i'm not sure what will work, this is part of my question) named grades. In a for command which I already have worked out, when the user is prompted for a grade:
    Code:
    scanf("%whatever", grades);
    If they type in an A, the computer will know that A means 4.00, if they type B, the computer will know that B means 3.00, then I'm going to have a floating point for the variable totalgrades and it will be something like
    Code:
    totalgrades+=grades;
    What I cannot figure out is how to assign the inputted letters numbers.

    EDIT: If you want to see the code, I'll paste it but I didn't want to waste space.
    Last edited by eLliDKraM; 08-21-2005 at 09:04 AM.

  2. #2
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    char, float, long, int, double, short, byte, long double... all will work
    just scanf("%whatever", &grade);
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    So you're saying if I did:

    Code:
    float grades;
    
    blah blah
    
    scanf("%f", &grades);
    if(grades='A') {
    grades=4.00;
    totalgrades+=grades;
     }
    It would work?

  4. #4
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    well yes. but it's an ugly way of doing it.
    Code:
    int totalgrade;
    char x, no_of_grades,i;
    int main(void) {
         for (i=0;i<no_of_grades;i++) 
              switch(x=toupper(getchar())) {
                   case 'A': {totalgrade+=4;break;}
                   ...
                   }
         printf("%d", (float)totalgrade/(float)no_of_grades);
         return(0);
         }
    Last edited by moonlord; 08-21-2005 at 09:26 AM.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    The user will input the grade letters.

  6. #6
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    1. because you only read 1 letter at once, it's 'neater' to use getchar instead.
    2. printf("%d", (float)totalgrade/(float)no_of_grades); does a thing called "casting", or changing the way c treats the casted variable. if you only used totalgrade/no_of_grades that would be a integer division (3.0 / 2.0 = 1.5, but 3 / 2 = 1 -- see the difference?)
    3. you'll need to include ctype and conio, for toupper() and getchar()
    Code:
    #include <ctype.h>
    #include <conio.h>
    4. toupper will make sure that it will work if the user inputs capital or small case letters.
    5. if you need more than 127 grades to make an avg, make totalgrade a int or long (mind those who don't have the patience to type in 2 giga of data ).
    Last edited by moonlord; 08-21-2005 at 09:28 AM.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    Thanks. The tutorial on http://www.cprogramming.com/fod/getchar.html regarding getchars doesn't really help, do you know any other places with basic tutorials?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by moonlord
    2. printf("%d", (float)totalgrade/(float)no_of_grades); does a thing called "casting", or changing the way c treats the casted variable. if you only used totalgrade/no_of_grades that would be a integer division (3.0 / 2.0 = 1.5, but 3 / 2 = 1 -- see the difference?)
    Here however, "%d" is used for integeral types, and not floating point numbers. So the cast really isn't going to do what you want anyway, because it's still going to display it as an integer, truncating anything past the decimal point anyway.
    Quote Originally Posted by moonlord
    3. you'll need to include ctype and conio, for toupper() and getchar()
    Code:
    #include <ctype.h>
    #include <conio.h>
    Actually, getchar is in <stdio.h>. You're thinking of getch, which is in <conio.h>, which is a non-ANSI function that only some compilers have. (Along with getche.) It's commonly used with people with old compilers in place of getchar, however getchar is a portable version, which behaves slightly different.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    missed the %d out there.
    the second problem: i don't argue with you, i have never actually used getchar.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do know they have private messaging here for this sort of thing, right?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 09:34 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM