简体   繁体   English

C ++不读IF / Else语句

[英]C++ not reading IF/Else Statement

Hi I'm a new programmer just learning the basics of C++. 嗨,我是一名刚学习C ++基础知识的新程序员。 I have created a very small section of code simply to practice the syntax of C++ and reinforce certain concepts. 我创建了一小段代码,只是为了练习C ++的语法并强化某些概念。 The program is a vending machine replica using if, else if and else statements to read the user input and display what they have choosen. 该程序是一个自动售货机副本,使用if,else if和else语句来读取用户输入并显示他们选择的内容。

But for some reason my entire block of If/Elses is being ignored. 但由于某种原因,我的整个If / Elses块被忽略了。 When I've tried the debugger stopping at any section of it the program simply doesn't stop and when I've put the debugger somewhere else and tried to insert the red stop dot into the if statements I get a message saying "no executable code is associated with this line." 当我尝试调试器在其任何部分停止时,程序根本不会停止,当我将调试器放在其他地方并尝试将红色停止点插入if语句时,我收到一条消息“没有可执行文件代码与此行相关联。“

Here is my code 这是我的代码

#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 below\n\n";

//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;
}

So yeah any help or suggestions would be appreciated. 所以,是的任何帮助或建议将不胜感激。

PS Sorry if I made any mistakes on this post it's my first of what will hopefully be many posts. PS对不起,如果我在这篇文章中犯了任何错误,这是我的第一篇希望很多帖子。 Any constructive criticism on how I made my post or even how I'm self-teaching my coding would be appreciated. 任何有关我如何发表我的职位的建设性批评,甚至是我如何自我教授我的编码都将受到赞赏。

"\nCoca-Cola";

should be 应该

cout << "\nCoca-Cola";

the same for the other lines. 对于其他线路也一样。

Currently your if\\else get executed but you simply don't do anything (at least nothing useful in them). 目前你的if\\else被执行但你根本就没有做任何事情(至少没有任何用处)。 You got string literals in your code that you simply don't do anything with. 你的代码中有字符串文字,你根本不做任何事情。

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

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