Thread: small problem but....

  1. #1
    Registered User
    Join Date
    Dec 2004

    small problem but....

    Hello guys,

    I want to know what my Visual .net compiler is skipping the process of the input of the book title after the first loop.

    #include <stdio.h>

    struct books
    char titel[50];
    char auther[50];
    float price;
    int isbn;
    } book_info[5];

    int main()
    int i;
    for (i=0; i<5; i++)

    printf("title: ");

    printf("auther: ");

    printf("price: ");
    scanf("%f", &book_info[i].price);

    printf("ISBN: ");
    scanf("%d", &book_info[i].isbn);


    for (i=0; i<5; i++)
    printf(" Price is %.3f and ISBN is %d", book_info[i].price , book_info[i].isbn);

    return 0;


    the problem at the red line.

    at the first loop everything gose well but the second loop the program dose not ask for the value of book_info[i].title but instead is asks
    for the book_info[i].aurther

    i'm not a begginer in programming but i'm in C and i can't find what is the problem

    any help

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    scanf() leaves a newline on the input buffer so when it gets back around to gets() it thinks the user just hit ENTER.

    Check the FAQ on this site for information on how to reliably get data from the user.

    Another thing: Don't use gets(). Ever. For any reason. An explanation of why it's bad is also in the FAQ.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2004
    thanks man.

    yes the problem is in scanf

    after i removed scanf from the loop it worked well.

    don't ask me why we are using gets() or whatever, it just our teacher is using it.


  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    don't ask me why we are using gets() or whatever, it just our teacher is using it
    I see this comment so often, in reference to so many bad things. I know some teachers don't like to be shown up, but most will accept a different way of doing things quite happily (assuming it doesn't affect the substance of what they're teaching you), so long as you can explain why you did something a certain way.

    Who knows, it may even be worth extra credit? Comment out the gets and rewrite it using fgets to show you know how to use both. Add a comment about why you did it, I'd be willing to bet $5.00 that your teacher will commend your work, not shoot you down for it. (Course you may have to come to Aussie to collect if I'm wrong).
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Registered User
    Join Date
    Dec 2004
    you are completly correct. I agree with you.

    But the point that this course is just basics of programming, he would not care much which function we use since we use a function that reads strings from user.

    the main problem that the exam (after few hours) is going to be paper-based exam and imagine if he asks the same thing i did...about gets()...then i can't not answer the question saying

    forgets gets() sir it is bad ...use fgets()

    i will get zero....

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Sure you can. You can do it in a sentence or two:
    gets is a bad choice because it does no boundry checking. fgets specifies the size of the input buffer and is the better choice by far.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM