Я кодирую на C++ с помощью Visual Studios, и мой код будет принимать только файл под названием «perm15K.txt». Если я попытаюсь ввести «perm30K.txt» или «sorted15K.txt», мой код не будет читать из него. Он не выводит ошибку с файлом, но он не позволит мне ввести, какой поиск я хотел бы выполнить.Код будет принимать только один пользовательский входной файл вместо других
#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 в конце, и это не имело значения. Текстовые файлы находятся в папке исполняемого файла и решения и классов. –
Когда вы действительно заботитесь о получении отладочного сообщения, используйте 'cerr', а не' cout'. – user4581301