'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
    Location
    Trinidad & Tobago
    Posts
    43

    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.

    Code:
    #include<stdio.h>
    #include<conio.h>
     
    struct product
        {
        char name[30];
        int stock;
        float price, discount;
        };
     
    void main()
    {
     
         struct product p[3];
         int i;
         float temp;
        // clrscr();
     
         for(i=0;i<3;i++)
         {
            printf("Enter product name :");
            gets(p[i].name);
     
            printf("\nEnter Stock :");
            scanf("%d",&p[i].stock);
     
            printf("\nEnter Price :");
            scanf("%f",&temp);
            p[i].price = temp;
     
            printf("\nEnter Discount :");
            scanf("%f",&temp);
            p[i].discount = temp;
     
            printf("\n\n");
           // fflush(stdin);
         }
     
        // clrscr();
         for(i=0;i<3;i++)
         {
         printf("Name=%s, Stock=%d, Price=$%.2f, Discount=%.2f%.\n", p[i].name, p[i].stock, p[i].price,p[i].discount);
         }
         getch();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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
    Posts
    69
    Code:
    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, 07:05 PM
  2. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  3. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  4. Function Error
    By otchster in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2007, 11:00 AM
  5. function calling within another function error
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 12:40 AM

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