c++不读取IF/Else语句

C++ not reading IF/Else Statement

本文关键字:Else 语句 IF 读取 c++      更新时间:2023-10-16

大家好,我是一名刚刚学习c++基础知识的新程序员。我创建了一小段代码,只是为了练习c++的语法和强化某些概念。该程序是自动售货机的复制品,使用if、else if和else语句读取用户输入并显示他们所选择的内容。

但由于某种原因,我的整个If/else块被忽略了。当我尝试调试器在它的任何部分停止时,程序根本不会停止,当我将调试器放在其他地方并试图在if语句中插入红色停止点时,我得到一条消息说"没有可执行代码与这一行相关联"。

我的代码

#include <iostream>
using namespace std;
int main ()
{
//Greets the user
cout << "Welcome to the cola machine" << endl;
cout << "Please choose your beverage from the list belownn";
//Lists the options for the user
cout << "1 - Coca-Cola" << endl;
cout << "2 - A & W Root Beer" << endl;
cout << "3 - Sprite" << endl;
cout << "4 - Pepsi" << endl;
cout << "5 - Mountain Dew" << endl;
int pop;
cin >> pop;
if (pop == 1)
{
    "nCoca-Cola";
}
else if (pop == 2)
{
    "nA & W Root Beer";
}
else if (pop == 3)
{
    "nSprite";
}
else if (pop == 4)
{
    "nPepsi";
}
else if (pop == 5)
{
    "nMountain Dew";
}
else
{
    "nError. choice was not valid, here is your money back.";
}
//Pauses program at end because I'm lazy
system("pause");
return 0;
}

所以,是的,任何帮助或建议都会很感激。

注:抱歉,如果我在这篇文章中犯了任何错误,这是我的第一个,希望会有更多的帖子。对于我如何写这篇文章,甚至是我如何自学编码,任何建设性的批评都将不胜感激。

"nCoca-Cola";
应该

cout << "nCoca-Cola";

与其他行相同。

当前你的ifelse被执行,但你根本不做任何事情(至少没有任何有用的)。你的代码中有字符串字面值你根本不用它