C++ if 语句故障

发布于 2024-12-15 04:48:08 字数 800 浏览 3 评论 0原文

如果我输入 Artist,我也会从 Artist 得到 if 以及 else 语句。我看不出问题是什么,它编译得很好,但仅在大写字母 Artist 上,它给了我对 Artist 的双重价值,一切都很好。

cin >> answer;

if (answer == "Artist") {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
    cout << "Playing music like a boss" << endl;
    Drums = (Drums + 1);
    Base = (Base + 1);

}
if (answer == "artist") {

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
    cout << "Playing music like a boss" << endl;
    Drums = (Drums + 1);
    Base = (Base + 1);
}
else {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    cout << "You should buy a guitar" << endl;
    Drums = (Drums + 1);
    Bag = (Bag - 1);
}

[编辑] 好的,我将代码更改为使用 if 语句,但问题仍然存在。

If I enter Artist I will get the if from the Artist plus the else statement too. I cant see what the problem is it compiles fine but only on capital letter Artist it gives me double value on artist all is good.

cin >> answer;

if (answer == "Artist") {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
    cout << "Playing music like a boss" << endl;
    Drums = (Drums + 1);
    Base = (Base + 1);

}
if (answer == "artist") {

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
    cout << "Playing music like a boss" << endl;
    Drums = (Drums + 1);
    Base = (Base + 1);
}
else {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    cout << "You should buy a guitar" << endl;
    Drums = (Drums + 1);
    Bag = (Bag - 1);
}

[EDIT] Okay, I changed the code to using if statements but the problem remains.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余生一个溪 2024-12-22 04:48:08

现在的情况是,如果字符串包含“Artist”,那么您将执行该块,“artist”检查失败并执行 else 块。

您很可能想要:

else if (answer == "artist") {

这将使得只有一个块将被执行。

The way it is now, if the string contains "Artist" then you will execute that block, fail the check for "artist" and execute the else block.

You most likely want:

else if (answer == "artist") {

This will make it so only one of the block will be executed.

肩上的翅膀 2024-12-22 04:48:08

您不能在 switch 语句中使用字符串。 switch 中只能使用整型常量。您必须使用一系列 if 语句。

You can't use strings in switch statements. Only integral constants can be used in switch. You'll have to use a series of if statements.

二智少女猫性小仙女 2024-12-22 04:48:08

这是如何运作的?您无法打开字符串,必须使用字符串相等性来检查。

switch 语句 编译为跳转表,其中输入本质上是选择跳转中的目标表被采取。所以你的开关输入必须是一体型的。不是字符串。

How does this work? You cannot switch on a string, you have to use string equality to check.

A switch statement compiles down to a jump table, where the input is essentially choosing which target in the jump table is taken. So your switch input has to be of an integral type. Not a string.

如此安好 2024-12-22 04:48:08

C++ 不是我最擅长的语言,但我理解使用带有 String 的 switch 具有未定义的行为。

C++ is not my strongest language, but my understanding that using switch with a String has undefined behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文