1

I know that there are already a few questions similar to this one but I'm truly not getting what I'm doing wrong in my case.

I have to collect 5 variables, 3 are strings that can be more than one word, and the other 2 are intergers.

This is for a program that I need to turn in and the input need to be exactly like I posted below.

I also need to specifically use sscanf and fgets and not scanf or getchar().

int main()
{
    char linha[200];
    char * nome;
    char * autor;
    char * genero;
    char anoChar[200];
    int * ano;
    char duracaoChar[200];
    int * duracao;

    printf(">");

    fgets(linha, sizeof linha, stdin);
    sscanf(linha, "%[^\n]", nome);

    fgets(linha, sizeof linha, stdin);
    sscanf(linha, "%[^\n]", autor);

    fgets(linha, sizeof linha, stdin);
    sscanf(linha, "%[^\n]", genero);

    fgets(linha, sizeof linha, stdin);
    sscanf(linha, "%[^\n]", anoChar);
    *ano=atoi(anoChar);

    fgets(linha, sizeof linha, stdin);
    sscanf(linha, "%[^\n]", duracaoChar);
    *duracao=atoi(duracaoChar);

    printf("%s %s %s %d %d", nome, autor, genero, ano, duracao);

    return 0;
}

I've tried putting on the input per example:

  Dreams
  Singer
  pop
  1970
  10

But it will stop reading on "Dreams" and just close the program

pmaed
  • 35
  • 4
  • 1
    Does this answer your question? [fgets doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf) – Hitokiri May 18 '20 at 14:32
  • TLDR: There's a new line character in the input buffer. – Code-Apprentice May 18 '20 at 14:34
  • 1
    The pointers (`nome`, `autor`, `genero`, `ano`, `duracao`) do not point somewhere valid. – pmg May 18 '20 at 14:35
  • 1
    So the first `fgets` fails? Or the first `fgets` works but the second blocks? What about the `sscanf`? What are the contents of `linha` and `nome`? Why don't you check any return values? It's important to learn how to look carefully at your code and how to debug it. – Useless May 18 '20 at 14:35
  • @Useless After I type the first input "Dreams", the compiler stops, it won't read anything more. I use ` linha ` as buffer and I don't check any return values because I don't want that to be the return. – pmaed May 18 '20 at 14:41
  • The compiler is not running at this time. The program (which you already finished compiling) stops - which means you can see _where_ it stops in your debugger. Every time you call a system or library function that _can_ return an error code, you should check the return value to see whether it succeeded. Currently you have a string of operations, any of which could fail, and you don't know where it got to or what state anything is in. You can, and should, learn to inspect this in your debugger. – Useless May 18 '20 at 14:45
  • 2
    use arrays instead of pointers (to nowhere valid) `char nome[200]; char autor[200]; char genero[200]; int ano[1]; int duracao[1];` no further changes required. – pmg May 18 '20 at 14:45
  • thanks @pmg it solved it :) – pmaed May 18 '20 at 14:49

2 Answers2

2

These variables:

char * nome;
char * autor;
char * genero;
int  * ano;
int  * duracao;

May be your problem. They are pointers that have not been initialized before first use, therefore are invoking undefined behavior the second they are called. This means they may point to memory that may already be allocated for use by another process, or worse may point to memory that is not currently used thus allowing the access to go without visible problems making you think your code is working, but is really a ticking time-bomb.

Either use arrays,

char nome[80]={0};
char autor[80]={0};
char genero[80]={0};
... etc.

or initialize the pointers to point to something:

char * nome = malloc(80);
if(nome)
{... //test before using allocated memory
     //... do same with others
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

@pmg Sugested changing the pointers into arrays and it worked.

pmaed
  • 35
  • 4