2012-05-26 69 views
0

我想,只要你運行一個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; 
} 
+5

好的。讓我們把事情弄直。這是一個**控制檯窗口**。 – chris

+1

也許你應該學習你正在使用的東西的名字,如果你要編寫軟件。在Windows中,這是[控制檯窗口](http://en.wikipedia.org/wiki/Win32_console),而不是*黑盒子*。 –

+1

函數通常使用camel case命名:'printToScreen()'。我也看到了以大寫爲首的全球功能的論點。另外,你的變量通常應該存在於你的函數中,並作爲參數傳遞。 – chris

回答

1

你是不是叫你printoscreen功能。嘗試在main()函數結束之前添加printtoscreen();

編輯:

而且考慮改變int printoscreen(){void printoscreen(){和correspondigly return 0;只是return;在此功能,不必返回任何有意義的東西,並ignorig在主要結果值。因此,編碼將是:

#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; 
} 
+0

它給了我一個「printtoscreen標識符找不到」的錯誤 – forthewinwin

+1

對不起,我的壞...我應該編輯後立即工作。 –

+0

作品。非常感謝你的幫助! – forthewinwin

6

也許,如果你真的稱爲printtoscreen,你可能會發現,它會執行暫停代碼。

但是,實際上,無論如何,我都會在main的末尾放置cin.get()位,只是因爲它只有在IDE中運行時纔有。你可能不想在最終的可執行文件中使用它,因爲它可能會惹惱任何試圖運行它的人。

換句話說,從printtoscreen末尾移除cin.get();,並把這樣的事情在main末:

​​

而且請記住,您可能需要main之前,無論是移動printtoscreen,或者在main之前提供原型(以便main知道它)。

+2

在這裏,我正在查看'istreambuf'是否在輸入緩衝區中保留換行符以及Linux是否有'pause'命令。我真的需要把我的優先事項弄清楚。 – chris

+0

在你不想評論的時候,爲什麼會有'printtoscreen()'函數呢? – chris

+0

我正在做的任務是要求我在控制檯窗口中顯示矢量的內容。 – forthewinwin