C++ 如何退出if语句并返回while的顶部
我有一个循环结构,我试图解析一个文件。我想从一个if语句跳到while循环的顶部。然而,当我使用continue时,我希望发生的事情不会发生。使用“断开”时,整个while循环结束。:/有人知道当一个条件满足时,我如何回到循环的顶端吗?我的代码如下:C++ 如何退出if语句并返回while的顶部,c++,algorithm,C++,Algorithm,我有一个循环结构,我试图解析一个文件。我想从一个if语句跳到while循环的顶部。然而,当我使用continue时,我希望发生的事情不会发生。使用“断开”时,整个while循环结束。:/有人知道当一个条件满足时,我如何回到循环的顶端吗?我的代码如下: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <vector> usi
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void parseActivityFile()
{
//read in the file
ifstream infile;
int last_act = 0;
int acts = 0;
string line;
vector<string> activs;
int current_int = 0;
int curr_activity = 0;
infile.open("activities.txt");
while (getline(infile, line))
{
if (line.find("===") != string::npos)
{
cout << "line = " << line << endl;
++current_int;
if (current_int == 2)
{
string last_activs = activs.back();
cout << "The last activity is: " << last_activs << endl;
cout << "This is last activity # " << ++last_act << "\n\n";
activs.clear();
current_int = 0;
}
continue; //this continue statement does not jump to the top of the while
}
{
if (line.find("activity") != string::npos)
{
cout << "Activity found: " << line << endl;
cout << "This is activity # " << ++acts << endl;
activs.push_back(line);
}
}
}
return;
}
int main()
{
parseActivityFile();
return 0;
}
输出如下所示:
你的括号错了。我已经在下面修好了 例1:
你的问题陈述归结为当我使用“继续”时,我想发生的事情没有发生,这是不可接受的。请提出一个简明的问题——这里的问题必须能够在将来使其他人受益。你有什么证据表明继续下去不会回到循环的顶端。“看起来应该是这样。”阿贝伦基:这是个好问题。表示if line.find==的行不会立即运行。相反,该程序似乎下降到它下面的一行,该行显示if line.findactivity.想必这是因为读取的下一行不包含===。@paddy hmmm可能你是对的。好吧。0_o。所以,也许它是跳回顶端,但我的程序仍然没有做我想做的。你怎么知道这是他们寻找的逻辑?他们没有提供示例输入,我想我无法确定用户3870315想要什么逻辑。但看看提供了什么,我做了一个有根据的猜测。我编辑了我的问题,添加了一个要解析的示例文件。我认为这是我试图解析的文件的一个合适的例子。这是因为这个用户完全尝试了你的问题。这种情况发生在:a您不提供示例输入;b你没有提供预期的产出;c用户觉得提供错误的答案比要求澄清更有帮助。可能问题在于您正在进行随机尝试,而不是执行一系列逻辑调试步骤来缩小和隔离问题。试着阅读一些关于调试技术的指南。
======================
blah blah blah
11010101 x - - -
kdkjueureiruer
activity "today on mars..."
stuff:
more data in the file i do not care about
======================
blah blah blah
11010101 x - - -
kdkjueureiruer
activity "today on mars..."
stuff:
more data in the file i do not care about
======================
blah blah blah
11010101 x - - -
kdkjueureiruer
activity "today on mars..."
stuff:
more data in the file i do not care about
======================
while (getline(infile, line))
{
if (line.find("===") != string::npos)
{
cout << "line = " << line << endl;
++current_int;
if (current_int == 2)
{
string last_activs = activs.back();
cout << "The last activity is: " << last_activs << endl;
cout << "This is last activity # " << ++last_act << "\n\n";
activs.clear();
current_int = 0;
continue; //this continue statement does not jump to the top of the while
}
if (line.find("activity") != string::npos)
{
cout << "Activity found: " << line << endl;
cout << "This is activity # " << ++acts << endl;
activs.push_back(line);
}
}
}
while (getline(infile, line))
{
if (line.find("===") != string::npos)
{
cout << "line = " << line << endl;
++current_int;
if (current_int == 2)
{
string last_activs = activs.back();
cout << "The last activity is: " << last_activs << endl;
cout << "This is last activity # " << ++last_act << "\n\n";
activs.clear();
current_int = 0;
}
else if (line.find("activity") != string::npos)
{
cout << "Activity found: " << line << endl;
cout << "This is activity # " << ++acts << endl;
activs.push_back(line);
}
}
}