简体   繁体   English

在线编译器(在线 gdb)工作正常,而 g++ 和 gcc on windows 10 cmd 在比较字符串时不起作用

[英]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).当我在网站“在线 gdb”中比较 2 个字符串时,如果比较相等,则给出 10,如果不相等,则不给出 10(它给出 10 而不是 0 已经很奇怪了,但它有效,所以我没有'不在乎)。 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.但是由于我尝试使用 g++ 或 gcc 编译我的代码(我也尝试使用 -g 来编译它们)它给出了 -1 或 1,或者只有 1,但从来没有 0。感谢所有阅读这段代码并帮助我的人。

#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最后我只是用 scanf 替换了 fgets,现在它可以工作了......我不知道为什么我没有从一开始就这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 glibc rand()不适用于python,但在在线编译器中可以正常工作 - glibc rand() doesn't work with python but works fine in online compiler 用gcc / g ++编译器编译c ++ - compiling c++ with gcc/g++ compiler 在 vscode 中出现分段错误,但在在线编译器(gdb)中没有 - Getting segmentation fault in vscode but not in online compiler (gdb) 正则表达式在C中不匹配,但可与在线解释器一起使用 - Regex doesn't match in c but works with online interpreter 此代码可在线运行,但不能从我自己的编译器运行 - This code works online, but not from my own compiler 为什么比较C中的字符串不起作用? - Why comparing strings in C doesn't work? gcc / g ++警告,如果比较两个指针的地址而不是内容? - gcc/g++ warning if comparing two pointers' addresses instead of contents? 在 GCC/G++ 编译器中使用 -pedantic 的目的是什么? - What is the purpose of using -pedantic in the GCC/G++ compiler? 如何在OSX上安装gtk以与g ++ / gcc编译器一起使用 - How to install gtk on OSX for use with g++/gcc compiler 通过分析程序集列表验证gcc / g ++中的编译器优化 - Verifying compiler optimizations in gcc/g++ by analyzing assembly listings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM