I'm learning and practicing 'C'. Using fgets() function has been a challenge for me. For example, the below code is running absolutely fine. When I run this piece of code, it takes my mom's name (firstName lastName) as input but the '.' is being placed on the next line. How can I get the period on the same line of the output?
#include <stdio.h>
int main()
{
char motherName[30];
printf("Enter your mother's full name: ");
fgets(motherName, sizeof(motherName), stdin);
printf("Your mom's full name is %s.", motherName);
return 0;
}
There's also another query. When using fgets() function along with other variable inputs, it's leading to a bug. The first two inputs are taken in properly and outputs are exactly the way that I wanted it. But when it comes to the fgets(), it isn't taking any input. I hope you'd help me out with it. I'm also new to stack overflow, thanks for helping!
#include <stdio.h>
int main()
{
int num;
printf("What's your favorite number: ");
scanf("%d", &num);
printf("Your favorite number is %d.\n", num);
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Your name is %s.\n", name);
char motherName[30];
printf("Enter your mother's full name: ");
fgets(motherName, sizeof(motherName), stdin);
printf("%s is your mom's full name.", motherName);
return 0;
}
As per the documentation, fgets()
fills the buffer including the terminating LF (newline) character.
To dispose of the newline (linefeed) after fgets()
:
#include <string.h> // add this header to use MANY string functions
/* ... */
fgets(motherName, sizeof motherName, stdin); // "()" unnecessary with sizeof in this usage.
motherName[ strcspn(motherName, "\n") ] = '\0'; // add this line to obliterate LF
As for the second problem, it's a wrestling match to use both scanf()
and fgets()
together. The former will leave (unexpected) whitespace (including LF) in the buffer. And, as you've done, most code doesn't check that scanf()
assigned all the values expected.
Now that you're using fgets()
, stop using scanf()
for input. The Standard Library contains all the functions you need to separate strings into tokens (strtok()
) and to convert those tokens to int
(strtol()
) or double
(strtod()
). This is the way the big boys do it.
Move away from the keyboard and go learn about the functions provided by the Standard Library.
EDIT
Your code modified:
#include <stdio.h>
#include <string.h> // Added
int main()
{
char motherName[30];
printf("Enter your mother's full name: ");
fgets(motherName, sizeof(motherName), stdin);
motherName[ strcspn(motherName, "\n") ] = '\0'; // obliterate LF
printf("Your mom's full name is %s.\n", motherName); // added LF to this output
return 0;
}
Result:
Enter your mother's full name: She who cooks dinner
Your mom's full name is She who cooks dinner.
Second problem:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char buf[ 128 ]; // some size big enough
printf("What's your favorite number: ");
fgets( buf, sizeof buf, stdin );
int num = strtol( buf, NULL, 10 ); // new function. read doco
printf("Your favorite number is %d.\n", num);
printf("Enter your name: ");
fgets( buf, sizeof buf, stdin );
buf[ strcspn(buf, "\n") ] = '\0'; // << NB
printf("Your name is %s.\n", buf);
printf("Enter your mother's full name: ");
fgets( buf, sizeof buf, stdin );
buf[ strcspn(buf, "\n") ] = '\0'; // << NB
printf("%s is your mom's full name.\n", buf);
return 0;
}
Result:
What's your favorite number: 27
Your favorite number is 27.
Enter your name: fe2O3
Your name is fe2O3.
Enter your mother's full name: Marge Simpson
Marge Simpson is your mom's full name.
Can't say why you're having problems...