Q:

using gets(str) in C

Im trying to use gets(str) in my code 
here is my code 
 #include<stdio.h>

int main(void){

    int i; //item number
    char coins[100]; //getting coins using get(str)
    printf("(Item#  ItemName   ItemPrice)\n");
    printf("1.     Water       0.55TL\n");
    printf("2.     Cola        0.55TL\n");
    printf("3.     Fanta       0.55TL\n");
    printf("4.     Chocolate   0.55TL\n");
    printf("5.     Biscuit     0.55TL\n");
    printf("6.     Gum         0.55TL\n");
    printf("7.     Candy       0.55TL\n");
    printf("Enter your selection item => ");
    scanf("%d",&i);
    printf("Your choice is %d", i);
    printf("\nEnter coins => ");
    gets(coins);
    printf("\nYou have entered: %s coins", coins);

    return 0;   
    }

actually program suppose to ask 2 inputs as one is item number (digit) and another one is coins that user had entered. To make sure the program is working right i try to print those i and couns but program asking only 1 input and assigning that input to both couns and i depend on char or integer that i have entered.  Whats wrong with this code ? what should i do to separate those coins and item number?  Sorry for my bad english but i think u got what i mean  Thanks

A:

Generally , gets its a bad way to read strings. If you want so, you have to know that gets reads ENTER Key a string, which scanf translates as a '\n' char, so after scanf("%d",&i); command, gets(coins) reads as next string the character returned from ENTER key pressed, so in your programm, all you have to do is to place an extra `gets(coins) command before the existing gets you already have.
your programm should be like this 
#include<stdio.h>

int main(void){

    int i; //item number
    char coins[100]; //getting coins using get(str)
    printf("(Item#  ItemName   ItemPrice)\n");
    printf("1.     Water       0.55TL\n");
    printf("2.     Cola        0.55TL\n");
    printf("3.     Fanta       0.55TL\n");
    printf("4.     Chocolate   0.55TL\n");
    printf("5.     Biscuit     0.55TL\n");
    printf("6.     Gum         0.55TL\n");
    printf("7.     Candy       0.55TL\n");
    printf("Enter your selection item => ");
    scanf("%d",&i);
    printf("Your choice is %d", i);
    printf("\nEnter coins => ");

    gets(coins); //extra gets 
    gets(coins);
    printf("\nYou have entered: %s coins", coins);

    return 0;   
}

