我希望每当你运行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;
}
答案 0 :(得分: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();
请注意,您可能需要将printtoscreen
移至main
之前,或在main
之前为其提供原型(以便main
知道它)。
答案 1 :(得分:1)
您没有呼叫printoscreen
功能。尝试在printtoscreen();
功能结束前添加main()
。
编辑:
另请考虑在此函数中将int printoscreen(){
更改为void printoscreen(){
并相应地return 0;
更改为return;
,因为您没有返回任何有意义的内容,并且在主要内容中是ignorig结果值。所以enitre代码将是:
#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;
}