简体   繁体   English

用C ++打印结构

[英]printing structs in C++

I'm trying to print 3 attributes from a struct. 我正在尝试从结构中打印3个属性。 Why won't they all print? 他们为什么都不打印? They will print 2 at a time, but not all three together. 它们将一次打印2张,但不会同时打印三张。

name has a Carriage Return character at the end. name的末尾有回车符。 So it's printing 所以是印刷

2HP Potion\r10

\\r moves the cursor to the beginning of the line, without moving to the next line, so 10 overwrites 2H . \\r将光标移动到该行的开头,而不移动到下一行,因此10覆盖2H

I suspect this is because you read the name from a file that was written on Windows, which uses \\r\\n as its line break sequence in text files. 我怀疑这是因为您从Windows上编写的文件中读取了名称,该文件在文本文件中使用\\r\\n作为其换行顺序。 You should either fix the file using dos2unix , or change the code that reads the file to remove \\r characters. 您应该使用dos2unix修复文件,或者更改读取文件的代码以删除\\r字符。

You can remove the \\r at the end with: 您可以使用以下命令删除\\r

int last_pos = name.size()-1;
if (last_pos >= 0 && name[last_pos] == '\r') {
    name.pop_back();
}

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

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