这些continue语句如何影响我的代码
How are these continue statements affecting my code?
我一直在Visual Basic 2013中编写这个小程序,试图为用户输入命令创建一种分层结构。基本上,我希望两个单词输入中的第一个能将程序引导到一个代码区域,并为第二个单词提供一组响应。在这个程序中,第一个单词可以是"人类"或"动物"。这些单词将程序引导到选择动物或人类类型的函数。
#include "stdafx.h"
#include <iostream>
#include <sstream>
void ifAnimal(std::string b) //This is the set of responses for a first word of "Animal"
{
if (b == "pig")
{
std::cout << "It is a pig." << std::endl;
}
if (b == "cow")
{
std::cout << "It is a cow." << std::endl;
}
}
void ifHuman(std::string b) //This is the set of responses for a first word of "Human"
{
if (b == "boy")
{
std::cout << "You are a boy." << std::endl;
}
if (b == "girl")
{
std::cout << "You are a girl." << std::endl;
}
}
int main()
{
while (1)
{
std::string t;
std::string word;
std::cin >> t;
std::istringstream iss(t); // Set up the stream for processing
int order = 0;
//use while loop to move through individual words
while (iss >> word)
{
if (word == "animal")
{
order = 1;
continue; //something wrong with these continues
}
if (word == "human")
{
order = 2;
continue;
}
if (order == 1)
{
std::cout << "The if statement works" << std::endl;
ifAnimal(word);
}
if (order == 2)
{
std::cout << "This one too" << std::endl;
ifHuman(word);
}
}
}
return 0;
}
问题是,每当程序到达continue语句时,调用我的函数的if语句都不会被触发。根本不显示任何文本。如果continue语句被删除,则If语句会触发,但相应的函数包含错误的单词。我是不是没有意识到这些人在做什么?有没有更好的方法来完成我想做的事情?
continue
的意思是跳过循环的其余部分,返回顶部。如果CCD_ 3被命中,那么CCD_。
看起来您希望word
同时是两个单词,所以一旦执行ifAnimal()
,就不会满足ifAnimal
中的任何情况。当您调用该方法时,word
永远不会是"pig"
或"cow"
,因为只有当word
等于"animal"
时,您才会调用该方法,并且在此之后不会更改它。
Continue的意思是"立即转到循环的顶部,然后重新开始"。你根本不想那样。
//use while loop to move through individual words
while (iss >> word)
{
if (word == "animal")
{
order = 1;
}
else if (word == "human")
{
order = 2;
}
if (order == 1)
{
std::cout << "The if statement works" << std::endl;
ifAnimal(word);
}
if (order == 2)
{
std::cout << "This one too" << std::endl;
ifHuman(word);
}
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 使用"静态"如何影响我的代码速度?
- 额外的反斜杠字符不会影响我的程序.为什么
- 这些continue语句如何影响我的代码
- 我认为这是一个编译器错误,这不应该影响我的代码,但它是
- 代码重新排序会影响我的测试吗
- 我可以在不影响性能的情况下将此宏更改为内联函数吗
- 我正在更改我的代码,但编译的程序不受影响