1

I have this code:

char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");

printf("%s ", variable1);

This gets a string with the possibility of having spaces and assigns it to the variable variable1. Later, I can print the string with no problem.

The problem comes when I add to code other fgetsfor gets other string in other variable.

if (1) {
    scanf("%c",&temp);
    fgets(variable2,50,stdin);
    strtok(variable2, "\n");

    printf("%s ", variable2);
}

The result complete is:

char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");

printf("%s ", variable1);

if (1) {
    scanf("%c",&temp);
    fgets(variable2,50,stdin);
    strtok(variable2, "\n");

    printf("%s ", variable2);
}

variable1 always works correctly. But I try print in some phrases with %s variable2 but the result does not get the first character, only in second scanf. If I put HELLO, variable2 is ELLO.

I have tested using another temp variable, another data, etc. But always get the same error.

Why is this happening?

UPDATE For more information. I use scanf because, if I do not use it, the program does not pause while waiting for the string. I use strtok(variable1, "\n"); to remove the line break.

This program is inside a while and in switch case. I put the complete code:

case 4: printf( "Put equipo: "); 
    scanf("%c",&temp);
    fgets(equipo,50,stdin);
    strtok(equipo, "\n");

    if (Exists(equipo)) {
        printf("Put Piloto ");
        scanf("%c",&temp);
        fgets(piloto,50,stdin);
        strtok(piloto, "\n");
        printf("You said %s and %s", equipo, piloto);
    }

    break;

If I introduce like Equipo HELLO and like Piloto FRIEND, the output is:

You said HELLO and RIEND

  • 2
    What's the reason for `scanf("%c", &temp);`? If you're trying to skip over a newline, `fgets()` already reads that. – Barmar May 21 '20 at 19:06
  • Please post a [mcve], including all the variable declarations. – Barmar May 21 '20 at 19:07
  • 1
    You print `variable1` after scanning `variable2`, so your `variable1` may not be as "correct" as you think. – M Oehm May 21 '20 at 19:24
  • 1
    The `if()` has `printf("%s ", variable1);`. Is that the true code? Is it really `printf("%s ", variable2);`? – chux - Reinstate Monica May 21 '20 at 20:16
  • 1
    what do you want to happen if they type more than 50 characters? – M.M May 21 '20 at 22:08
  • the UPDATE differs from the original question. You originally said the 2nd fgets gave garbage, but now you say it gives the right string missing the first character (as is to be expected since you consumed that character with `scanf("%c"`). The updated code will work properly if you just remove the second `scanf("%c"` line (and don't type 50 characters in the first fgets). Please review your question and make sure about what you are really asking. It would be good to include a https://stackoverflow.com/help/minimal-reproducible-example – M.M May 21 '20 at 22:09
  • Have updated my answer based on your updated post, and information you provide in comments. – ryyker May 22 '20 at 13:54

2 Answers2

2

Re-written based on OP latest edits to post, and comments...

You describe the need to simply obtain two strings from user input, remove the newlines from each, then package both into a message to stdout. If this description actually matches what you need, change this code section:

...
scanf("%c",&temp);
fgets(equipo,50,stdin);
strtok(equipo, "\n");

if (Exists(equipo)) {
    printf("Put Piloto ");
    scanf("%c",&temp);
    fgets(piloto,50,stdin);
    strtok(piloto, "\n");
    printf("You said %s and %s", equipo, piloto);
...

To this:

...
printf("enter equipo: ");
if(fgets(equipo, sizeof(equipo), stdin))
{
    equipo[strcspn(equipo, "\n")] = 0; //remove newline 
    printf("enter piloto: ");
    if(fgets(piloto, sizeof(piloto), stdin))
    {
        piloto[strcspn(piloto, "\n")] = 0; 
        printf("You said %s and %s", equipo, piloto);
    }
}
...

Note: strtok(piloto, "\n"); works, but has problems if user just hits <return>

By the way, here are some other interesting ways to clear the newline

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • @M.M - I think I see your point. Bad choice of function and after OP updates to answer, would not have even addressed the main request. Total re-write. Thanks – ryyker May 22 '20 at 13:33
0

I do not understand your example code. Could you update the question with your actual code?

I assume scanf should not be there?

This code works fine for me:

char var1[50], var2[50];
fgets(var1, 50, stdin);
strtok(var1, "\n");
printf("Var1: %s\n", var1);
if (1) {
        fgets(var2, 50, stdin);
        strtok(var2, "\n");
        printf("Var2 %s\n", var2);
}

Output:

Test1
Var1: Test1
Test2
Var2 Test2
nraiha
  • 76
  • 5