我想,只要你運行一個C++程序留在彈出的控制檯窗口......但在我這裏的代碼沒有發生。它很快就消失了。怎麼了?注意:我是C++新手。控制檯窗口不會留在屏幕上,儘管「cin.get();」或系統(「暫停」)
由於某些原因,當我僅使用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;
}
好的。讓我們把事情弄直。這是一個**控制檯窗口**。 – chris
也許你應該學習你正在使用的東西的名字,如果你要編寫軟件。在Windows中,這是[控制檯窗口](http://en.wikipedia.org/wiki/Win32_console),而不是*黑盒子*。 –
函數通常使用camel case命名:'printToScreen()'。我也看到了以大寫爲首的全球功能的論點。另外,你的變量通常應該存在於你的函數中,並作爲參數傳遞。 – chris