Practical C Programming, Third Edition

This is a discussion on Practical C Programming, Third Edition within the C Programming forums, part of the General Programming Boards category; Hey all :-) I am new to the forums and have decided to try and learn C. The book I ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    Practical C Programming, Third Edition

    Hey all :-) I am new to the forums and have decided to try and learn C.

    The book I am using to teach myself C is "Practical C Programming, Third Edition" by O'Reilly.

    I am on chapter 6 and am stuck on programming exercise 6-2.

    Does anybody know of a website that has the answers to the exercises at the end of each chapter?
    Last edited by jc99; 06-19-2009 at 06:50 PM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    OK, I have figured out the answer to question 6-2. For anyone who wants to know here it is

    Code:
    #include <stdio.h>
    
    char    line[4];
    int     grade;
    
    
    
    int main ()
    {
    
            printf ("What is the numeric grade: ");
            fgets(line, sizeof(line), stdin);
            sscanf(line, "%d", &grade);
    
            if (grade <= 60)
                    printf("F\n");
    
            if (grade >= 61)
                    while (grade <= 70)
                    {
                    printf("D\n");
                    break;
                    }
    
            if (grade >= 71)
                    while (grade <= 80)
                    {
                    printf("C\n");
                    break;
                    }
    
            if (grade >= 81)
                    while (grade <= 90)
                    {
                    printf("B\n");
                    break;
                    }
    
            if (grade >= 91)
                    printf("A\n");
    return(0);
    }
    I am still interested in knowing if there are answers on some website to the questions at the end of each chapter.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    lol what?

    have you heard of else if? because loops that are guaranteed to only loop once make my eyes bleed

    Code:
    #include <stdio.h>
    
    int main ()
    {
            
            char    line[4];
            int     grade;
    
            printf ("What is the numeric grade: ");
            fgets(line, sizeof(line), stdin);
            sscanf(line, "%d", &grade);
    
            if (grade < 60)
                    printf("F\n");
            else if (grade < 70)
                    printf("D\n");
            else if (grade < 80)
                    printf("C\n");
            else if (grade < 90)
                    printf("B\n");
            else 
                    printf("A\n");
    return 0;
    }
    and what is line for anyway?
    Last edited by ಠ_ಠ; 06-19-2009 at 07:02 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    The book, so far, doesn't mention "else if". I am answering the question with what I have learned so far. I am aware there are probably more elegant ways of answering this question but I haven't learned that yet.
    Last edited by jc99; 06-19-2009 at 07:38 PM.

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by jc99 View Post
    The book, so far, doesn't mention "else if". I am answering the question with what I have learned so far. I am aware there are probably more elegant ways of answering this question but I haven't learned that yet.
    O.O

    I'd get a new book
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    Are you talking about

    Code:
    char line[4];
    I am still a little confused what I need it for. I thought I needed it so I can use fgets and sscanf to get the grade I entered into the program when I run it

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    It's the memory that you allocate to store input from stdin.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jc99 View Post
    The book, so far, doesn't mention "else if". I am answering the question with what I have learned so far. I am aware there are probably more elegant ways of answering this question but I haven't learned that yet.
    You should always check your facts. Given that "else" is covered in section 6.2, which is before the programming exercise under consideration.....

    You could be using line to validate input -- you went to all the trouble to read a string and then read out of string -- but you don't handle bad input. (Neither does the author of your book, so we'll forgive you this once.)

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    You should always check your facts. Given that "else" is covered in section 6.2, which is before the programming exercise under consideration.....
    Maybe I am missing it but I am re-reading chp6 (on page 85) and all I can find is...
    Code:
    if (condition)
       statement;
    else
       statement;
    Which is "if else" I see no mention of "else if". I was unaware I could do "else if" but will keep it in mind for future programs I write.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    else if is the same as
    Code:
    if (condition)
         statement;
    else
         if (condition2)
              statement;
    You can chain as many of them as you want.

    It's just more conventionally written as
    Code:
    if (condition)
         statement;
    else if (condition2)
         statement;
    To the compiler they are the same (remember that compilers ignore indentation).

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    And
    Code:
            if (grade >= 61)
                    while (grade <= 70)
    in your case, is the same as
    Code:
         if (grade >= 61)
              if (grade <= 70)
    which is the same as (but you might not have learned this yet)
    Code:
         if (grade >= 61 && grade <= 70)
    and then, if you use else, you can simplify it further to ಠ_ಠ's version. Only one condition is needed per letter grade, because, for example,
    Code:
     else if (grade < 70)
    won't be evaluated if grade < 60.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual c++ 2005 express edition to run .c files
    By the_fall_guy in forum C Programming
    Replies: 4
    Last Post: 04-05-2007, 12:33 PM
  2. Setting C++ express edition.
    By shiju in forum Game Programming
    Replies: 12
    Last Post: 08-15-2006, 05:55 AM
  3. cmd close's too soon...
    By dimirpaw in forum C++ Programming
    Replies: 21
    Last Post: 11-27-2005, 09:23 PM
  4. Win98 First Edition Share internet through router.
    By Bajanine in forum Tech Board
    Replies: 4
    Last Post: 10-17-2004, 02:43 AM
  5. Visual C++ .NET Standard Edition
    By knave in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2003, 09:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21