我想撰寫一段代碼,從幾個給定的引數串列中為 D&D 5e 創建一個隨機藥水串列。我幾乎完成了,除了一行代碼之外,每一位代碼都可以正常作業。
我期望這樣的輸出:“液體是:黃色,帶有顏色斑點。”。相反,我得到了這個:“帶有斑點的顏色。”。基本上,整個部分帶有:“液體是:”被省略了。奇怪的是,當顏色為“深紅色”時,它在一種情況下運行良好。
這是最小的作業示例:
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <fstream>
#include <sstream>
int main(int argc, char** argv)
{
std::vector<std::string> appearance;
std::vector<std::string> appearance_2;
std::ifstream d_appearance("appearance.txt");//20 entries
std::ifstream d_appearance_2("appearance_2.txt");//20 entries
std::string line;
std::string line_2;
for(int i = 0; i < 20; i )
{
getline(d_appearance, line);
appearance.push_back(line);
getline(d_appearance_2, line_2);
appearance_2.push_back(line_2);
}
std::random_device generator;
std::uniform_int_distribution<int> twenty_dis(0,19);
std::string p_appearance = appearance[twenty_dis(generator)];
std::string p_appearance_2 = appearance_2[twenty_dis(generator)];
for(int i = 0; i < 1; i )
{
std::ostringstream s_look;
s_look << "The liquid is; " << p_appearance << " with " << p_appearance_2;
std::string look = s_look.str();
std::cout << look << std::endl;
std::cout << std::endl;
}
return 0;
}
如果我只是將文本檔案作為代碼塊放在這里,我希望沒問題:外觀
Clear
Blue
Green
Red
Pale Green
Pink
Light Blue
White
Black
Dark Grey
Light grey
Yellow
Orange
Gold
Orange
Bronze
Metallic
Purple
Brown
Dark Red
外觀_2
flecks of colour.
swirls of colour.
fizzing bubbles.
bubbles suspended in it.
some kind of bone floating in it.
leaves and flowers in it.
two separated liquid phases.
a bright glow.
a soft glow.
stripes of colour.
translucency.
a cloudy murkiness.
blood within it.
dirt floating in it.
chunks of metal in it.
some type of gore from a slain creature.
steam coming from it.
a face in the liquid.
constantly moving and shifting liquid.
a constant heat.
uj5u.com熱心網友回復:
這可能是行尾的問題。如果您在 Windows 中創建了該檔案(因此您有"\r\n"行尾)并在 Linux 中使用該檔案,則其getline作業方式會有所不同。它將'\n'用作分隔符,但會被'\r'視為一個單獨的字串。因此,您可能會得到一些等于 的外觀"\r"。在一天結束時,您可以輸出:
std::ostringstream s_look;
s_look << "The liquid is; " << "\r" << " with " << p_appearance_2;
std::string look = s_look.str();
std::cout << look << std::endl;
std::cout << std::endl;
這將覆寫字串的開頭,因此您不會"The liquid is; "在輸出中看到。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403640.html
標籤:
