I'm messing around in C trying to learn a bit about how it works, and I ran into a problem. I have a structure defined with two character array variables. I'm populating them using fgets() from keyboard input. However when I go to print, the output looks like this:
Gibson
Les Paul
Fender
Stratocaster
When I really want it to look like this:
Gibson Les Paul
Fender Stratocaster
I can accomplish this just fine when using scanf opposed to fgets, but I figured I'd see if I can get some understanding on why this happens as I'm new to C.
Here is my code:
#include <stdio.h>
typedef struct Guitars
{
char brand[10];
char model[10];
} input;
void input_data(struct Guitars input[10])
{
for(int i=0; i<2; i++)
{
printf("Please enter the brand: ");
fgets(input[i].brand, 10, stdin);
printf("Please enter the model: ");
fgets(input[i].model, 10, stdin);
}
}
void print_array(struct Guitars input[10])
{
for(int i=0; i<2; i++)
{
printf("%s%s", input[i].brand, &input[i].model);
}
}
int main(void) {
struct Guitars input[10];
input_data(input);
print_array(input);
}
Thanks for any help!