2016-09-30 18 views
0

Visual Studioを使用してC++でコーディングしています。私のコードは "perm15K.txt"というファイルのみを受け入れます。私が "perm30K.txt"または "sorted15K.txt"を入力しようとすると、私のコードはそこから読み取られません。ファイルにエラーは出力されませんが、実行したい検索を入力することはできません。コードは、1つのユーザー入力ファイルのみを受け入れるようにします。

#include "stdafx.h" 
#include "binarysearchtree.h" 
#include "redblacktree.h" 
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <ctime> 
#include <chrono> 

using namespace std; 

int main() 
{ 
struct bstnodes *root = NULL; 

std::ifstream in; 
std::ofstream out; 

std::stringstream buffer; 
buffer << in.rdbuf(); 
std::string test = buffer.str(); 
std::cout << test << std::endl << std::endl; 

ifstream myFile; 
string input; 
string output; 

cout << "Name of the input file? (ie. perm15K.txt)"; 
getline(cin, input); 
myFile.open(input.c_str()); 

if (myFile.fail()) 
{ 
    cout << "Error with file\n"; 
} 

    for (int j = 0; j < 10; j++) 
    { 

     cout << "Which search? Pleast enter bst or redblack\n"; 

     binarysearchtree temp; 
     redblacktree temp1; 

     while (!myFile.eof()) 
     { 
      while (getline(myFile, input)) { 
       myFile >> input; 
       string words = input; 

       temp.insert(words); 
       temp1.rbinsert(words); 
      }  
     } 
     getline(cin, input); 
      if (input == "bst") 
      { 
       cout << "\nSearch for what word in the tree\n"; 
       getline(cin, input); 
       temp.insert(input); 

       clock_t start_s = clock(); 
       std::cout << "Match Found: " << temp.search(input) <<  std::endl; 
       clock_t stop_s = clock(); 
       double sum = ((double)(stop_s - start_s)); 
       cout << endl << "Time: " << sum << " seconds" << endl; 

       cout << "\nSearch for what word in the tree\n"; 
       getline(cin, input); 
      } 

      if (input == "redblack") 
      { 
       cout << "\nSearch for what word in the tree\n"; 
       getline(cin, input); 
       temp1.rbinsert(input); 
       clock_t start_s = clock(); 
       temp1.rbsearch(input); 
       std::cout << "Match Found: ";      
       clock_t stop_s = clock(); 
       double sum = ((double)(stop_s - start_s)); 
       cout << endl << "Time: " << sum << " seconds" << endl; 
       cout << "\nSearch for what word in the tree\n"; 
       getline(cin, input); 
      } 

     myFile.close(); 
    return 0; 
} 
} 

私の問題を理解する助けがあれば幸いです。それは私が無限ループのために立ち往生しているようなものです。私は数時間問題を見つけようとしていて、解決策を見つけることができません。

答えて

0

Visual Studioが実行ファイルを配置している場所を見つけて、他のテキストファイルがあるかどうかを確認してください。

また、あなたの印刷文は、< < "text" < < endlである必要があります。 "\ n"でそれらを終了すると、エラー出力を失う可能性があるプリントバッファを実際にフラッシュしていない可能性があります。

+0

最後に\ nを変更しても違いはありません。テキストファイルは実行可能ファイルと解決策とクラスフォルダにあります。 –

+0

デバッグメッセージを出すのに本当に気を使うときは、 'cout'ではなく' cerr'を使います。 – user4581301

1

myFile.getline()が期待どおりに動作しないため、ファイルのオープンに失敗した可能性があります(無限ループ)。

オープンに失敗した場合は、エラーメッセージの後に戻る必要があります。

また、eofを確認するのではなく、while(myFile.getline())とするだけです。

+0

これで意味することをコードで表示できますか? –

+2

'while(!myFile.eof())'行を削除するだけです。 –

+0

私はそれをして何も修正しませんでした。 –

関連する問題