如何正确格式化
How do I format this properly?
我正在执行"随机漫步"程序,无法正确显示条形图。
这是我的代码(我只会包括我需要帮助的内容):
#include <iostream>
#include <conio.h>
#include <string>
#include <ctime>
#include <random>
using namespace std;
int main() {
srand(time(0)); // make it "random"
int max;
int min;
int location; //location of goat on bridge
int steps;
int total_steps;
int choice;
int move; // go forward or backward
for (;;) { // Keep running until user quits
system("CLS");
cout << "Random Walk Simulator" << endl;
cout << "1) Display Trial " << endl;
cout << "2) Run Statistics Over 50 Trials " << endl;
cout << "3) Quit. " << endl;
cin >> choice; // get choice
cout << "n";
if (choice == 1) { // Display Trial
steps = 1;
location = 4; // center of bridge
cout << "Step 0: | * | " << endl;
while (location >= 1 && location <= 7) { // Keep running until goat falls off bridge
move = rand() % 2 + 0; // 0 and 1
if (move == 0) { // Move backward
location--;
}
if (move == 1) { // Move forward
location++;
}
switch (location) { // Show location
case 0: cout << "Step " << steps << ": | | " << endl;
break;
case 1: cout << "Step " << steps << ": |* | " << endl;
break;
case 2: cout << "Step " << steps << ": | * | " << endl;
break;
case 3: cout << "Step " << steps << ": | * | " << endl;
break;
case 4: cout << "Step " << steps << ": | * | " << endl;
break;
case 5: cout << "Step " << steps << ": | * | " << endl;
break;
case 6: cout << "Step " << steps << ": | * | " << endl;
break;
case 7: cout << "Step " << steps << ": | *| " << endl;
break;
case 8: cout << "Step " << steps << ": | | " << endl;
break;
default: break;
}
steps++; // Iterate steps
}
cout << "nTotal steps taken: " << (steps - 1) << endl; // (steps-1) since I start the goat off at the center of the bridge
system("PAUSE");
}
以下是输出示例:
Random Walk Simulator
1) Display Trial
2) Run Statistics over 50 Trials
3) Quit
1
Step 0: | * |
Step 1: | * |
Step 2: | * |
Step 3: | * |
Step 4: | * |
Step 5: | * |
Step 6: | * |
Step 7: | * |
Step 8: | * |
Step 9: | * |
Step 10: | * |
Step 11: | * |
Step 12: | * |
Step 13: |* |
Step 14: | |
如何正确格式化?对不起,我对C++有点陌生。
您需要放置额外的I/O操纵器来正确格式化输出。
只需更换
<< steps
带有以下代码
<< right << setw(2) << steps
在那些case
块中
case 0: cout << "Step " << steps << ": | | " << endl;
setw(2)
将输出字段宽度设置为2,而right
告诉将字段对齐在右边界。因此,您的输出看起来像
...
Step 8: | * |
Step 9: | * |
Step 10: | * |
Step 11: | * |
...
您还需要#include <iomanip>
。
我只需要使用一个选项卡
cout << "Step " << steps << ": | | " << endl;
更改为:
cout << "Step " << steps << ":t| | " << endl;
尝试使用setw()
setw引用
所以你的cout
应该看起来像
cout<<std::setw(9);
cout << "Step " << steps;
cout<<std::setw(0);
cout<<: "| *|";
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- CMake-按正确顺序将项目与C运行时对象文件链接
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 用于访问容器<T>数据成员的正确 API
- 如何使用Luacneneneba API正确读取字符串和表参数
- 如何使用用户输入在C++中正确填充2D数组
- node-gyp 在 macOS 上未正确链接库
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- 如何正确格式化csv文件?
- 如何正确格式化此打印功能
- 可变参数模板必须具有可调用的专用化,非空参数包才能正确格式化
- 正确格式化I2C的数据(Wire.write)
- 使用C中的多部分JSON正确格式化
- C++ << 使用ostream重载以正确格式化
- 格式化时间(使用timespec)以获得以纳秒为单位的正确值
- 如何正确格式化
- 如何正确格式化DEVPROP_TYPE_DATE
- 如何正确格式化c++浮点数到小数点后两位
- 如何正确格式化类实例引用