简体   繁体   English

C ++文件输入/输出输出数字而不是字符

[英]C++ File Input/Output Outputting Numbers Instead of Chars

I have created a program that randomly assigns roles(jobs) to members of a certain house using file input / output.. It builds successfully, but when using cout and I actually see the results, I can see why the program is not working. 我创建了一个程序,该程序使用文件输入/输出将角色(职位)随机分配给某个房屋的成员。它可以成功构建,但是当使用cout时,我实际上看到了结果,所以我可以看到为什么该程序无法正常工作。

Here is the snippet of code I believe something is wrong with : 这是我认为有些问题的代码片段:

  std::string foo = std::string("Preferences/") + std::to_string(members[random]) + "-Preferences";
  cout << foo << endl;

And here is the members[random] array, it is randomly selecting members from this array and reviewing their available times and assigning them jobs based on their Preference input file. 这是members [random]数组,它是从该数组中随机选择成员并查看其可用时间,并根据其“首选项”输入文件为其分配作业。

unsigned const char members[22] = 
{   'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v'  };

I have created a random number picker that goes through 0-21 and assigns the value it creates to variable random. 我创建了一个随机数选择器,它通过0-21并将其创建的值分配给变量random。 So, in essence, it is members[random] and completely random. 因此,从本质上讲,它是成员[随机]且完全随机。

Here is the output I get in my terminal. 这是我在终端中得到的输出。

Preferences/116-Preferences

But I set the output to do Preferences/ member[random] -Preferences. 但是我将输出设置为Preferences / member [random] -Preferences。 It is accessing a number and not my array chars. 它正在访问一个数字,而不是我的数组字符。

I created a cout << members[random]; 我创建了一个cout <<成员[随机]; right below it, and every time I run the program, I get 就在它下面,每次我运行该程序时,

  Preferences/107-Preferences   <---- A random number every time

  k                   <---- random letter every time.

So I know it must be accessing my random functions, but assigned it to numbers! 因此,我知道它一定在访问我的随机函数,但已将其分配给数字! How do I fix this so my proper output can be : 我如何解决这个问题,这样我的正确输出可以是:

 Preferences/t-Preferences

Please help me, and thanks! 请帮助我,谢谢!

"The more you overthink the plumbing, the easier it is to stop up the drain" - Scotty, Star Trek III “您对管道的考虑越多,就越容易堵塞排水管”-《星际迷航》三世Scotty

Declaring members to be unsigned char s does not accomplish anything useful. members声明为unsigned char不会完成任何有用的操作。 A simple char will suffice. 一个简单的char就足够了。 std::string already implements an overloaded + operator that takes a char parameter, so it's much easier than you thought it would be: std::string已经实现了带char参数的重载+运算符,因此比您想象的要容易得多:

const char members[22] = {
   'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v'  };

// ...

std::string foo = std::string("Preferences/") + members[random]
                  + "-Preferences";

There is no ::std::to_string(char) , only (among less close) ::std::to_string(int) . 没有::std::to_string(char) ,只有(除了更接近之外) ::std::to_string(int) So your character is actually converted to its numerical representation and you get your unwanted result. 因此,您的字符实际上已转换为数字表示形式,并且您得到了不需要的结果。

Try instead 试试吧

std::string foo("Preferences/");
foo = foo.append(1, members[random]).append("-Preferences");

Variant using string streams: 使用字符串流的变体:

ostringstream oss;
oss << "Preferences/" << members[random] << "-Preferences";
// get your string via:
oss.str();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM