0

我希望在您运行 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;
}
4

2 回答 2

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();

请记住,您可能需要移至printtoscreenbefore main,或者之前为其提供原型main(以便main了解它)。

于 2012-05-26T01:14:27.030 回答
1

你没有调用你的printoscreen函数。尝试在函数printtoscreen();结束之前添加。main()

编辑:

还可以考虑在此函数中更改int printoscreen(){void printoscreen(){并相应return 0;地更改为return;,因为您没有返回任何有意义的内容,并且忽略了 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

void printtoscreen();

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++;
        }
    }
    printtoscreen();
}

void 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;
}
于 2012-05-26T01:28:27.757 回答