0

I'm working on a project in C. I need to print four variables in the same line: 3 of them are integers and 1 is an array that stores a string. The problem that I'm having is when I try to print all in the same line. After I print the array it jumps lines. I don't want it to jump lines because I need all of the variables on the same line.

This is the start that I made:

#define MAX 100

struct cadastro
{
  int id;
  char nome[MAX];
  int datanascimento;
  int cpf;
};

This is how I'm trying to print it:

printf("ID: %i  Nome: %s  Data de nascimento: %i  CPF: %i", cdto[c].id, cdto[c].nome, cdto[c].datanascimento, cdto[c].cpf); 

This is what I receive as feedback from printing:

ID: 0  Nome: Nilton
  Data de nascimento: 2  CPF: 2

I'm using fgets() to get the name typed.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nilton F
  • 21
  • 5
  • 1
    Value of `nome` probably ends in a newline? – erik258 Oct 27 '19 at 00:57
  • 1
    Don't store the newline in the name (`nome`) array. At the moment you do; it is printed as a newline. Remember that `fgets()` keeps the newline (if there's space enough for it). Use `cdto[c].nome[strcspn(cdto[c].nome, "\n")] = '\0';` to safely zap the trailing (well, first) newline. – Jonathan Leffler Oct 27 '19 at 00:57
  • Are you using `fgets` to read the string? If so, it reads a newline into the string which is what is getting printed, so you need to remove the newline after reading. – dbush Oct 27 '19 at 00:58
  • Yes, I'm using fgets – Nilton F Oct 27 '19 at 00:58
  • How could I remove the Newline? – Nilton F Oct 27 '19 at 00:59

2 Answers2

0

As I'm using fgets() to get the Name typed, I simply had to add a function:

strtok(cdto[c].nome, "\n");
Nilton F
  • 21
  • 5
  • no, `strtok` is too expensive for this simple purpose. Besides, it's designed to be called multiple times to get all all the tokens so if you just call it once the internal buffer may not be deallocated in some implementations, resulting in memory leak – phuclv Oct 27 '19 at 01:07
  • Ohh, I get it, thank you – Nilton F Oct 27 '19 at 01:13
  • @phuclv — can you cite an implementation of `strtok()` that leaks memory as you suggest? It sounds improbable — `strtok()` only needs a single `char *` that it uses to store the end of the last token it found. – Jonathan Leffler Oct 27 '19 at 02:22
  • @JonathanLeffler I don't know about the details of implementations, it's just a possible theory because `strtok` stores an internal state and it may allocate memory to return the next token – phuclv Oct 27 '19 at 02:41
  • @phuclv — I don't think that's a permissible problem. An implementation of a function such as that is not allowed to leak memory, as a QoI (quality of implementation) issue, if nothing else. The standard doesn't say "thou shalt call `strtok()` at least twice, once with a non-null pointer and at least once with a null pointer (in that sequence)". – Jonathan Leffler Oct 27 '19 at 02:44
0

To remove the trailing new line just use

nome[strlen(nome) - 1] = '\0';
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Note that if the string is empty, this ends up writing before the start of the buffer. It's not a particularly plausible problem, but there are [better ways to do it](https://stackoverflow.com/questions/58575857/print-an-string-inside-an-array-without-jumping-lines#comment103467602_58575857). – Jonathan Leffler Oct 27 '19 at 02:20