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;
}
}
私の問題を理解する助けがあれば幸いです。それは私が無限ループのために立ち往生しているようなものです。私は数時間問題を見つけようとしていて、解決策を見つけることができません。
最後に\ nを変更しても違いはありません。テキストファイルは実行可能ファイルと解決策とクラスフォルダにあります。 –
デバッグメッセージを出すのに本当に気を使うときは、 'cout'ではなく' cerr'を使います。 – user4581301