Like Tree2Likes
  • 2 Post By kermit

Beginner Question -- From Absolute Beginner's Guide to C

This is a discussion on Beginner Question -- From Absolute Beginner's Guide to C within the C Programming forums, part of the General Programming Boards category; I have begun using Absolute Beginner's Guide to C to learn the C programming language, and I am trying to ...

  1. #1
    Registered User
    Join Date
    Dec 2013
    Location
    Florida
    Posts
    3

    Beginner Question -- From Absolute Beginner's Guide to C

    I have begun using Absolute Beginner's Guide to C to learn the C programming language, and I am trying to work through examples in the book. However, I don't seem to be able to get the below copied program (page 31) to run. I realize that the scanf function doesn't play nice with getchar, but it's what I am being told to use at this point. I am hoping someone can give me some pointers here. Tried running after successful compilation in Dev C++ and the command line. Result for avg I am getting is a very large random number.

    Code:
    * Written by David Ghelerter, finished on December 25, 2013*/
    /* Filename: avg.exe*/
    /* Computes an average*/
    
    
    #include <stdio.h>
    main()
    {
       float gr1, gr2, gr3; /* Variables to hold grades */
       float avg; /* Variable holds averages*/
       /* Asks for a students grades */
       printf("What grade did the first student get ?\n");
       scanf(" %.1f", &gr1);
       fflush(stdin);
       printf(" What grade did the second student get ?\n");
       scanf(" %.1f", &gr2);
       fflush(stdin);
    
       printf("What grade did the thrird student get ?\n");
       scanf("%.1f", &gr3);
       fflush(stdin);
     
       avg = (gr1 + gr2 + gr3)/3.0;
       printf("\nThe student average is %.2f", avg);
       getchar();
       return 0;
    
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    To start, try changing your calls to scanf() from:

    Code:
    scanf("%.1f")
    to

    Code:
    scanf("%f")
    While scanf has provision for field width (i.e., you can limit the amount of characters taken in), it does not do precision (in other words, it is not analogous to printf() where you could specify how many digits after the decimal point are displayed). Your best bet might be to consult your local documentation for the scanf() function.

    Google Search: man scanf
    Last edited by kermit; 12-25-2013 at 06:41 PM.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Location
    Florida
    Posts
    3
    Excellent! Thanks for your insight

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    It would also be useful to check the return value from scanf() - it tells you how many values it succeeding in reading.

    Also, if your book is guiding you to use fflush(stdin), burn it. fflush() has undefined behaviour for any input stream. It does not necessarily, contrary to what poor books or library documentation often say, discard pending input.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    @ the OP: grumpy is right about the book you are using. Unfortunately it seems that there are multiple poor introductory C books published for every good one. One of the challenges of writing a tutorial type book is to quickly get the reader writing programs that work, in order to keep things interesting. After all, who wants to spend time writing and compiling programs that don't really do anything?

    Yet one has to walk before they can run. The quickest (easiest?) way to get some input in C would be either the gets() function or (more often seen) the scanf() function. Yet these pose their own problems, and what often accompanies these functions in example code are hacky solutions to make them "work." As grumpy noted, fflush(stdin) is not a great way of doing things.

    What is the best course of action then? As grumpy hinted, get a better book if possible. I would recommend The C Programming Language: Second Edition. It's a bit old now - it covers C89 standard C - yet there is still much there that is helpful. If you were to get that book, I would recommend reading Steve Summit's Notes to Accompany The C Programming Language, by Kernighan and Ritchie (``K&R'') along with it. Steve Summit is the maintainer of the comp.lang.c C-FAQ.

    And speaking of FAQs, you should familiarize yourself with these three (in order):

    1. comp.lang.c C-FAQ
    2. cprogramming.com C and C++ FAQ
    3. CPWiki FAQ


    Being familiar with these will help you to recognize bad code practice. For example, all three answer questions about using fflush(stdin):

    comp.lang.c FAQ:
    • 12.26a How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?
    • 12.26b If fflush won't work, what can I use to flush input?

    cprogramming.com FAQ:

    CPWiki FAQ:


    There is a lot of reading there - perhaps it is overwhelming - but don't be discouraged. Remember you have to walk before you can run, but don't stop at walking.
    Last edited by kermit; 12-25-2013 at 09:22 PM.
    Salem and stahta01 like this.

  6. #6
    Registered User
    Join Date
    Dec 2013
    Location
    Florida
    Posts
    3
    Kermit and Grumpy:
    Thanks very much for your insightful comments and help getting started. I'll familiarize myself with the materials you suggested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Absolute beginner would really appreciate some help
    By J. of KYC in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 07:56 AM
  2. Absolute beginner!
    By Leaghaire in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2007, 03:18 PM
  3. Replies: 14
    Last Post: 12-06-2006, 03:58 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM
  5. Absolute Beginner's Guide to Programming
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2003, 02:20 PM

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