1

when i launch this 2 lines of code

printf("|%-16s|%-16s|%-16d|\n","name","surname", 30);
printf("|%-16s|%-16s|%-16d|\n", database_publishers[pointer1].name, database_publishers[pointer1].client, database_publishers[pointer1].price);

it gives me output like this:

|name            |surname         |30              |
|name
           |surname
        |30              |

image:
image

variables in second printf call were got from user in other function, using fgets call like this

fgets(database_publishers[pointer].name, STRING_SIZE, stdin);

there are no any \n in this function and i have no idea why does it print every variable in new line. btw i work in visual studio, maybe it matters, idk, im just a rookie in programming

CiaPan
  • 9,381
  • 2
  • 21
  • 35
f4ll1nnn
  • 11
  • 2
  • Please post your output as text and not as image so that the question will not break in the future and it is easier to read/copy. Can you check the length of the strings that you are printing? – mame98 Dec 01 '21 at 23:24
  • 1
    `fgets()` also saves the **newline** character in the buffer, but we can only guess... – alex01011 Dec 01 '21 at 23:24
  • @mame98 ive added output as text. i just copied it from console, but im not sure if i ve done it right – f4ll1nnn Dec 01 '21 at 23:28

3 Answers3

1

Text that is read using fgets does most-likely contains a newline at the end. You can test for this and remove it if required. Try to add the following piece of code before you try to print out the result:

size_t length = strlen(database_publishers[pointer].name);
if(database_publishers[pointer].name[length - 1] == '\n')
    database_publishers[pointer].name[length - 1] = '\0';

Also, see this question, the above is basically this awnser.

Otherwise, if there is a newline character, your output will look like this:

|name            |surname         |30              |
|name<newline>
<remaining whitespace> | <...>
mame98
  • 1,271
  • 12
  • 26
  • It looks like name and surename both contain newlines, which seems to be supported by the answer that the OP just posted (using strcspn to remove the newline) – mame98 Dec 01 '21 at 23:39
  • 1
    You are indeed correct, the pattern I was thinking of would only occur it the column was also overflowing. Apologies! I have removed my incorrect comments and deleted my answer. – idz Dec 01 '21 at 23:51
0

I added this:

database_publishers[pointer].name[strcspn(database_publishers[pointer].name, "\n")] = 0;
database_publishers[pointer].client[strcspn(database_publishers[pointer].client, "\n")] = 0;

after fgets calls and now it works like it should i've found it here: Removing trailing newline character from fgets() input

f4ll1nnn
  • 11
  • 2
-1

You are using "\n" in the program. Try removing it