Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C programming fgets help please?

I am given assessment to use fget(not scanf or anything else)I made a menu and when I want to enter a choice fgets doesnt work, it shows error. I am working on it for 5 hours still didnt get it. Even it has not mentioned in textbook in detail about fgets. and

int main(void)

{

int optionStats[NUM_OPTION_STATS];

char choice;

do

{

printf("Menu\n\n");

printf("1. Translating alphabetic number\n");

printf("2. Fibonacci numbers\n");

printf("3. Sort\n");

printf("4. Find substrings\n");

printf("5. Session summary\n");

printf("3. Bushfire\n");

printf("6. Exit\n");

printf("7. Go Again\n");

printf("Enter your chose: ");

fgets(choice,size(choice),stdin);

fgets(choice,sizeof(choice),stdin);<<<<<<<doesnt work

if(choice=='1'){

char input[10];

printf("%s","Enter Alphabetic Phone Number:");

fgets(input,sizeof(input),stdin);<<<<<<doesnt work at all

translateNumber(0, input);

char c[1];

printf("\nPress enter to continue..");

fgets(c,1,stdin); <<<doesnt work

}

while(choice!='7')

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    fgets() returns a value which is a pointer to your targer ('input' in your example) or a null if no data was sent. Try enclosing each fgets in an if structure to handle the input as normal or perform appropriate error handing when a null returned.

Still have questions? Get your answers by asking now.