简体   繁体   中英

online compiler(online gdb)works correctly while g++ and gcc on windows 10 cmd doesn't work when comparing strings

When I compare 2 strings in the website "online gdb", if the comparison is equal it gives 10, if it's not it doesn't give 10(It's already weird that it gives 10 instead of 0, but it works so I didn't care). But since I tried to compile my code with g++ or gcc(I tried with -g too for both)it gives -1 or 1, or only 1, but never 0. Thanks to everyone which will read this code and will help me.

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
    char v1q[55], v2q[55], v3q[55], v4q[55];
    float v1, v2, v3, v4, vr, min, max;
    int comparison;
    int v3b = 1;

printf("Per la prima banda non esiste l'oro e l'argento;\nPer la seconda banda non esiste l'oro e l'argento;\nPer la terza banda non esiste il bianco;\nPer la quarta banda esistono SOLO il bianco, oro e argento.\n\n");

printf("Prima cifra: ");
fgets(v1q, sizeof(v1q), stdin);


if(strcmp(v1q, "nero") == 0)
{
    printf("\nNon esiste un valore per il nero, per la prima banda");
}
if(strcmp(v1q, "marrone") == 0)
{
    v1 = 10;
}
if(strcmp(v1q, "rosso") == 0)
{
    v1 = 20;
}
if(strcmp(v1q, "arancione") == 0)
{
    v1 = 30;
}
if(strcmp(v1q, "giallo") == 0)
{
    v1 = 40;
}
if(strcmp(v1q, "verde") == 0)
{
    v1 = 50;
}
if(strcmp(v1q, "blu") == 0)
{
    v1 = 60;
}
if(strcmp(v1q, "viola") == 0)
{
    v1 = 70;
}
if(strcmp(v1q, "grigio") == 0)
{
    v1 = 80;
}
if(strcmp(v1q, "bianco") == 0)
{
    v1 = 90;
}
if(strcmp(v1q, "oro") == 0)
{
    printf("\nNon esiste un valore per l'oro, per la prima banda");
}
if(strcmp(v1q, "argento") == 0)
{
    printf("\nNon esiste un valore per l'argento, per la prima banda");
}
comparison = strcmp(v1q, "marrone");
printf("%d", comparison);
}

In the end I just replaced fgets with scanf, and now it works... I don't know why I didn't do that from the start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM