0

I'm working on a function that appends data from the user and adds it to a text file in csv format. My problem is when it susposed to prompt me for the char variable it skips it and ask me for the product name instead. Does my logic look right here. Thanks for any help in advance.

my structure looks like this:

struct inventory_s
{
    int productNumber;
    float mfrPrice;
    float retailPrice;
    int numInStock;
    char liveInv;
    char productName[PRODUCTNAME_SZ];
};

int addProduct(void)
{

    struct inventory_s newInventory;

    FILE* inventoryFile = fopen("inventory.txt", "a"); //opens and writes to file
    if (inventoryFile == NULL) //if file is empty
        {
            printf("Could not open data file\n");
            return -1;
        }

    printf(" Please enter the new product number: ");
    scanf(" %i", &newInventory.productNumber);
    //input= 7000
    printf("\nPlease enter the manufacturer's price: ");
    scanf(" %f", &newInventory.mfrPrice);
    //input= 35
    printf("\nPlease enter the retail price: ");
    scanf(" %f", &newInventory.retailPrice);
    //input= 89.99
    printf("\nPlease enter the initial number in stock: ");
    scanf(" %i", &newInventory.numInStock);
    //input= 2
    printf("\nIs this a live animal? (1 for YES or 0 for NO): ");
    scanf(" %c",&newInventory.liveInv);
    //input= 1
    printf("\nPlease enter the new product name (up to 20 characters): ");
    scanf(" %[^\n]s", newInventory.productName);
    //input= Siamese Kitten
    fprintf(inventoryFile,"\n%i, %.2f,%.2f,%i,%c,%s",newInventory.productNumber, newInventory.mfrPrice, newInventory.retailPrice, newInventory.numInStock, newInventory.liveInv, newInventory.productName);
    fclose(inventoryFile);
    return 0;
}

OUTPUT: to inventory.txt

1000, 1.49,3.79,10,0,Fish Food
2000, 0.29,1.59,100,1,AngelFish
2001, 0.09,0.79,200,1,Guppy
5000, 2.40,5.95,10,0,Dog Collar Large
6000, 49.99,129.99,3,1,Dalmation Puppy 
7000, 35.00,89.99,2,1,Siamese Kitten
RTriplett
  • 23
  • 6
  • (1) [Why `gets()` is too dangerous to be used, ever](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). (2) The `gets()` reads the newline left behind by the prior `scanf()` call. This is a standard problem. – Jonathan Leffler Aug 08 '16 at 03:43
  • Thanks for the repile. I search and haven't found a duplicate. Thanks for sharing that with me. – RTriplett Aug 08 '16 at 04:05
  • Question: Why do you think it's skipping over my scanf() for &newInventory.liveInv? – RTriplett Aug 08 '16 at 04:14
  • Because when it reads `numInStock`, there's a newline left behind, which the `%c` reads. Use `" %c"` to skip leading white space. – Jonathan Leffler Aug 08 '16 at 04:18
  • Thanks a lot for taking the time to look at my problem! – RTriplett Aug 08 '16 at 04:28
  • FIXED the char scanf() and the string scanf()! – RTriplett Aug 08 '16 at 05:04
  • 1
    NB: The code in the question has been updated to show the resolution of the problems. The leading spaces are not necessary, but not harmful either, for the numeric inputs. Only the `%c`, `%[…]` and `%n` formats do not skip leading white space. The spaces before `%c` and `%[…]` therefore are important (and the scan set replaces a call to `gets()`). – Jonathan Leffler Aug 08 '16 at 05:10

0 Answers0