0

C++ プログラムを実行するたびにポップアップするコンソール ウィンドウを維持したいのですが、私のコードではそれが起こっていません。それはすぐに消えます。どうしたの?注:私はC ++が初めてです。

何らかの理由で、関数のみを使用main()してすべてを保持し、2 番目の関数を使用しない場合は適切に機能しますが、割り当てのためにすべてを に詰め込むことはできません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た場合、一時停止するコードが実行されることに気付くかもしれません。

しかし、実際には、IDE で実行しているときにのみ使用できるものであるため、とにかくcin.get()最後にそのビットを配置します。main実行しようとする人を困らせる可能性が高いため、最終的な実行可能ファイルには含めたくないでしょう。

cin.get();つまり、の末尾から削除して、 の末尾に次のprinttoscreenようなものを追加しmainます。

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

printtoscreenまた、 beforeに移動するか、事前mainにプロトタイプを提供する必要がある場合があることに注意してくださいmain(それについてmain知っているように)。

于 2012-05-26T01:14:27.030 に答える
1

関数を呼び出していませんprintoscreenprinttoscreen();関数が終了する前に追加してみてくださいmain()

編集:

また、意味のあるものは何も返さず、mainの結果値を無視するため、この関数だけに変更int printoscreen(){void printoscreen(){て対応することを検討してください。したがって、全体のコードは次のようになります。return 0;return;

#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 に答える