使用std :: cerr时,字符会被扰乱

时间:2018-05-01 17:52:32

标签: c++ unicode character ascii

我目前正在为一所学校的项目工作,无论我对输入文件或实现做什么,我都无法让它工作。

该代码在Visual Studio 2017中运行良好,但现在我在Linux环境中使用g ++。

getLineFromTextFile(std :: string textOrCin):

std::string getLineFromTextFile(std::string textOrCin) {
std::string currentLine;

if (textOrCin == "text") {
    if (!inputStream.eof()) {
        std::getline(inputStream, currentLine);
        //as long as currentLine's first two digits are "//..."
        while ((currentLine[0] == '/' && currentLine[1] == '/') || currentLine == "") {
            std::getline(inputStream, currentLine);
        }
        std::cout << currentLine << "\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(400));
        //std::this_thread::sleep_for(std::chrono::milliseconds(0));
    }
    else {
        std::cerr << "[global::getLineFromTextFile]: Critcal Error; End of file found, can't load anymore!\n";
        std::cerr << "exiting...";
        throw;
    }
}
else if (textOrCin == "cin") {
    std::cin >> currentLine;
}
else {
    std::cerr << "[global::getLineFromTextFile]: Critcal Error; Invalid 'textOrCin' input.\n";
    std::cerr << "exiting...";
    throw;
}
return currentLine;
}

功能实施:

void ZoinkersEngine::displayMainMenu(User& currentUser, std::string textOrCin) {
std::string option = "";
if (currentUser.getRole() == "admin") {
    do {
        std::cout << std::string(40, '\n');
        std::cout << "Successfully logged in...\n\n\n";
        std::cout << "Main Menu:\n";
        std::cout << "[0] Order Plan\n";
        std::cout << "[1] Generate Plan\n";
        std::cout << "[2] Manage Profile\n";
        std::cout << "[3] Manage Exhibits\n";
        std::cout << "[4] Manage Animals\n";
        std::cout << "[5] Manage Users\n";
        std::cout << "[6] Search Animal/Exhibit by Name\n";
        std::cout << "[7] Record Favorability\n";
        std::cout << "[8] Log Animal/Exhibit Care\n";
        std::cout << "[9] Add or Remove an Exhibit\n";
        std::cout << "[10] Add or Remove an Animal\n";
        std::cout << "[Cancel] To exit\n\n";

        std::cout << "Please input number of selected option: ";
        // std::cin >> option;
        option = getLineFromTextFile(textOrCin);

        //DEBUG START
        std::cerr << "Option: [" << option "].\n";
        std::cerr << "Option.size(): [" << option.size() "].\n";
        //DEBUG END


        if (option == "0") {
            currentUser.calculateExhibitFav(zoinkersDirectory);
            currentUser.orderPlan(zoinkersDirectory, textOrCin);
        }
        else if (option == "1") {
            currentUser.calculateExhibitFav(zoinkersDirectory);
            currentUser.generatePlan(zoinkersDirectory, textOrCin);
        }
        else if (option == "2") {
            currentUser.manageProfile(zoinkersDirectory, textOrCin);
        }
        else if (option == "3") {
            zoinkersDirectory.manageExhibit(currentUser.getUsername(), textOrCin);
        }
        else if (option == "4") {
            zoinkersDirectory.manageAnimal(currentUser.getUsername(), textOrCin);
        }
        else if (option == "5") {
            zoinkersDirectory.manageUsers(currentUser.getUsername(), textOrCin);
        }
        else if (option == "6") {
            zoinkersDirectory.searchAnimalExhibit(textOrCin, currentUser);
        }
        else if (option == "7") {
            currentUser.favorabilityUI(zoinkersDirectory, textOrCin);
        }
        else if (option == "8") {
            currentUser.logExhibitCare(textOrCin);
        }
        else if (option == "9") {
            zoinkersDirectory.addRemoveExhibit(currentUser.getUsername(), textOrCin);
        }
        else if (option == "10") {
            zoinkersDirectory.addRemoveAnimal(currentUser.getUsername(), textOrCin);
        }

        else if (option == "cancel" || option == "Cancel") {
            break;
        }
    } while (option != "cancel" || option != "Cancel");
}

调试输出:

Successfully logged in...


Main Menu:
[0] Order Plan
[1] Generate Plan
[2] Manage Profile
[3] Manage Exhibits
[4] Manage Animals
[5] Manage Users
[6] Search Animal/Exhibit by Name
[7] Record Favorability
[8] Log Animal/Exhibit Care
[9] Add or Remove an Exhibit
[10] Add or Remove an Animal
[Cancel] To exit

Please input number of selected option: 0
]ption: [0
option.size(): [2]

我应该只是进入&#34; 0&#34;,但在&#39; option.size()中看到:[2]&#39;有一些额外的东西弄乱了它。

即使它只是文本文件中的空行,它也会拉出那个奇怪的加扰器字符(大小应为0但是为1但仍然是加扰)。

我唯一的想法是我在我的.txt文件中使用了一个怪异的新行字符,而g ++并不能识别。 (.txt是在崇高的文本编辑器中制作的。)

我已经尝试将我的代码和文本文件复制到记事本中保存,关闭,重新打开并粘贴到emacs中,但仍然没有运气。

注意:所有功能都适用于带VS和G ++的manaul输入。

非常感谢任何帮助!

编辑1: 我输出了一个文本文件的选项并得到了奇怪的字符,它是&#34; ^ M&#34;。

1 个答案:

答案 0 :(得分:-1)

解决:这正是我所说的。我的测试文档使用的是windows换行符,而不是Unix。

修复:使用在线转换器将.txt文件从Windows支持的字符转换为支持Unix字符的字符。

编辑#2:我使用了http://newline.nadav.org/

注意:我与上面分享的网站没有任何关系!