I made a program, and found out gets(), and used it, it was great, however when I compiled the program it said that gets is dangerous, so then I searched up an alternative, even though the gets() function made it have the correct output, for the future I would rather use an alternative which isn't dangerous, which is when I stumbled upon fgets(). I thought that the code would compile the same and have the same output, however none of my if statments were being outputted.
Heres the code:
#include <stdio.h>
#include <string.h>
int main()
{
char qOne[10];
char qTwo[10];
char guess[40] = "My guess is that you are thinking of a\0";
printf("TWO QUESTIONS\n");
printf("Think of an object, and i'll try to guess it.\n\n");
printf("Question 1) Is it an animal, vegetable, or mineral?\n");
printf("\n> ");
//gets(qOne);
fgets(qOne, 10, stdin);
printf("\nQuestion 2) Is it bigger than a breadbox? (yes/no)\n");
printf("\n> ");
//gets(qTwo);
fgets(qTwo, 10, stdin);
printf("\n");
if(strcmp(qOne, "animal") == 0 && strcmp(qTwo, "no") == 0 ||strcmp(qOne, "animal") == 0 && strcmp(qTwo, "No") == 0)
printf("%s squirrel.\n", guess);
else if(strcmp(qOne, "animal") == 0 && strcmp(qTwo, "yes") == 0 ||strcmp(qOne, "animal") == 0 && strcmp(qTwo, "Yes") == 0)
printf("%s moose.\n", guess);
else if(strcmp(qOne, "vegetable") == 0 && strcmp(qTwo, "no") == 0 ||strcmp(qOne, "vegetable") == 0 && strcmp(qTwo, "No") == 0)
printf("%s carrot.\n", guess);
else if(strcmp(qOne, "vegetable") == 0 && strcmp(qTwo, "yes") == 0 ||strcmp(qOne, "vegetable") == 0 && strcmp(qTwo, "Yes") == 0)
printf("%s watermelon.\n", guess);
else if(strcmp(qOne, "mineral") == 0 && strcmp(qTwo, "no") == 0 ||strcmp(qOne, "mineral") == 0 && strcmp(qTwo, "No") == 0)
printf("%s paper clip.\n", guess);
else if(strcmp(qOne, "mineral") == 0 && strcmp(qTwo, "yes") == 0 ||strcmp(qOne, "mineral") == 0 && strcmp(qTwo, "Yes") == 0)
printf("%s Camaro.\n", guess);
printf("\nI would ask you if I'm right, but I don't actually care.\n");
return 0;
}
The output of this code is (inputting the strings "animal", and "yes"):
TWO QUESTIONS
Think of an object, and i'll try to guess it.
Question 1) Is it an animal, vegetable, or mineral?
> animal
Question 2) Is it bigger than a breadbox? (yes/no)
> yes
I would ask you if I'm right, but I don't actually care.
However when I only use gets() instead of fgets() my code gives the correct output, which is:
TWO QUESTIONS
Think of an object, and i'll try to guess it.
Question 1) Is it an animal, vegetable, or mineral?
> animal
Question 2) Is it bigger than a breadbox? (yes/no)
> yes
My guess is that you are thinking of a moose.
I would ask you if I'm right, but I don't actually care.
How can I get the same output, with fgets()?