尽管"cin.get();",控制台窗口仍不会停留在屏幕上 或系统("暂停")

for*_*win 0 c++ console program-entry-point

我希望每当你运行C++程序时弹出控制台窗口......但是在我的代码中没有发生这种情况.它很快消失了.怎么了?注意:我是C++的新手.

出于某种原因,当我仅使用main()函数来保存所有内容而没有第二个函数时,它可以正常工作,但出于我的任务目的,我无法将所有内容都填入main().

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdio>
using namespace std;

ifstream file("maze.txt");
vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>())); // Imports characters from file
vector<char> path;                      // Declares path as the vector storing the characters from the file
int x = 18;                             // Declaring x as 18 so I can use it with recursion below
char entrance = vec.at(16);             // 'S', the entrance to the maze
char firstsquare = vec.at(17);          // For the first walkable square next to the entrance
vector<char> visited;                   // Squares that we've walked over already

int main()
{
    if (file) {
        path.push_back(entrance);               // Store 'S', the entrance character, into vector 'path'
        path.push_back(firstsquare);            // Store the character of the square to the right of the entrance
                                                // into vector 'path'.
        while (isalpha(vec.at(x)))
        {
            path.push_back(vec.at(x));
            x++;
        }
    }
}

int printtoscreen()
{
    cout << "Path is: ";                    // Printing to screen the first part of our statement

        // This loop to print to the screen all the contents of the vector 'path'.
        for(vector<char>::const_iterator i = path.begin(); i != path.end(); ++i)  // 
        {
        std::cout << *i << ' ';
        }

        cout << endl;
        cin.get();                          // Keeps the black box that pops up, open, so we can see results.
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 6

也许如果您实际调用过 printtoscreen,您可能会发现它执行暂停的代码.

但是,事实上,无论如何,我都会把这cin.get()一点放在最后main,因为它只是在IDE中运行时才有的东西.你可能不会在最终的可执行文件中想要它,因为它可能会惹恼任何试图运行它的人.

换句话说,cin.get();从结尾处移除printtoscreen并在下面放置这样的东西main:

cout << "Press ENTER to exit (remember to remove this from production code)"
     << endl;
cin.get();
Run Code Online (Sandbox Code Playgroud)

请记住,您可能需要先移动printtoscreen到之前main,或者之前提供原型main(以便main了解它).

  • 在这里,我一直在寻找`istreambuf`是否在输入缓冲区中留下换行符以及Linux是否有`pause`命令.我真的需要优先考虑我的优先事项. (2认同)