c++不读取IF/Else语句
C++ not reading IF/Else Statement
大家好,我是一名刚刚学习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
被执行,但你根本不做任何事情(至少没有任何有用的)。你的代码中有字符串字面值你根本不用它
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 如果 else 语句不断循环
- C++ If/Else 语句被跳过
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如果 else 语句使用大写和小写字母
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 嵌套的 else 语句 c++
- 为什么切换 if else 语句的顺序会导致错误?
- 如何使用 if else 语句
- if/else 语句输出由于加号或减号而未显示正确的消息
- if-else 语句仅按特定顺序工作,我不知道为什么
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 如何在C++中为 if 和 else 语句定义新行为
- 如何在 if-else 语句中使用 C++20 的可能/不太可能属性
- While循环和if/else语句工作不正常