'gets function error'

This is a discussion on 'gets function error' within the C Programming forums, part of the General Programming Boards category; my program doesn't return any errors but if u compile it (please do) u will see that something is wrong. ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Trinidad & Tobago

    Question 'gets function error'

    my program doesn't return any errors but if u compile it (please do)
    u will see that something is wrong. i think is because of the 'gets' function i used but i can't use the 'scanf' function because it only reads the first word please help me resolve this problem.It would be deeply appreciated.

    struct product
        char name[30];
        int stock;
        float price, discount;
    void main()
         struct product p[3];
         int i;
         float temp;
        // clrscr();
            printf("Enter product name :");
            printf("\nEnter Stock :");
            printf("\nEnter Price :");
            p[i].price = temp;
            printf("\nEnter Discount :");
            p[i].discount = temp;
           // fflush(stdin);
        // clrscr();
         printf("Name=%s, Stock=%d, Price=$%.2f, Discount=%.2f%.\n", p[i].name, p[i].stock, p[i].price,p[i].discount);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Use int main, not void main

    Use fgets(), not gets()

    See the FAQ's
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    You need an extra fgets at the bottom of the loop (or something else to consume the newline).

  4. #4
    Registered User
    Join Date
    Dec 2011
    scanf("%[^\n]"); // Will read everything up until the newline character.
    That is just an example, it is still recommended that you only use scanf() for getting number inputs.

    As mentioned fgets() is better than gets() as you can specify the amount of chars you want to accept (protects you from buffer overflows).

    Your problem is that the product name variable is being consumed with the newline character. You need to find a way to flush the input buffer.
    FAQ > Flush the input buffer - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 08:05 PM
  2. Replies: 8
    Last Post: 07-08-2011, 02:16 PM
  3. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 12:53 PM
  4. Function Error
    By otchster in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2007, 12:00 PM
  5. function calling within another function error
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 01:40 AM

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