基于其他变量格式化字符串
Formatting strings based on other variables
正在做一个类的作业,它要求我们输入,这样我们就可以根据输入的数字创建一个"畜栏"。我只是很难根据另一个变量得到正确的间距。栏应该看起来像这样:
|==|==|==|
: :
- -
: :
|==|==|==|
我的问题是在中心的空白空间(这应该基于顶部/底部的长度)。Setw()在某种程度上是有效的,但同样,只是一个令人恼火的时间。这是我目前为止剩下的内容:
#include <iostream>
#include <iomanip>
using namespace std;
main () {
int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
char choice;
theloop = 0;
while (theloop < 1) {
do {
cout << "How many North/South Fence posts? ";
cin >> nsfp;
if (nsfp < 2)
cout << "Value must be at least 2, please try againn";
else if (nsfp > 10)
nsfp = 10;
} while (nsfp < 2);
do {
cout << "How many East/West Fence posts? ";
cin >> ewfp;
if (ewfp < 2)
cout << "Value must be at least 2, please try againn";
else if (ewfp > 10)
nsfp = 10;
} while (ewfp < 2);
for (count = 1; count <= nsfp; count++) {
cout << "|";
nsfp2 = nsfp - 1;
for (count = 1; count <= nsfp2; count++) {
cout << "==|";
nsfpw = count;
}
}
for (count = 1; count < ewfp; count++) {
cout << "n:" << " " << ":" << endl;
ewfp2 = ewfp;
for (count = 1; count < ewfp2; count++){
cout << "-" << setw(nsfpw) << "-" << "n:" << ":" << endl;
}
}
cout << "n" << nsfp << endl;
cout << ewfp;
theloop = 1;
}
}
也许你正在寻找的是这个。空格数为
3 * NSFP - 4;
#include <iostream>
#include <iomanip>
using namespace std;
main () {
int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
char choice;
theloop = 0;
while (theloop < 1) {
do {
cout << "How many North/South Fence posts? ";
cin >> nsfp;
if (nsfp < 2)
cout << "Value must be at least 2, please try againn";
else if (nsfp > 10)
nsfp = 10;
} while (nsfp < 2);
do {
cout << "How many East/West Fence posts? ";
cin >> ewfp;
if (ewfp < 2)
cout << "Value must be at least 2, please try againn";
else if (ewfp > 10)
nsfp = 10;
} while (ewfp < 2);
for (count = 1; count <= nsfp; count++) {
cout << "|";
nsfp2 = nsfp - 1;
for (count = 1; count <= nsfp2; count++) {
cout << "==|";
nsfpw = count;
}
}
cout << "n";
int spaces = 3 * nsfp - 4;
for (count = 1; count < ewfp; count++) {
cout << ":";
for (int i = 0; i < spaces; i++) {
cout << " ";
}
cout << ":n-";
ewfp2 = ewfp;
for (int i = 0; i < spaces; i++){
cout << " ";
}
cout << "-n";
}
for (count = 1; count <= nsfp; count++) {
cout << "|";
nsfp2 = nsfp - 1;
for (count = 1; count <= nsfp2; count++) {
cout << "==|";
nsfpw = count;
}
}
cout << "n" << nsfp << endl;
cout << ewfp;
theloop = 1;
}
}
更简洁的方法:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
main () {
int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
char choice;
theloop = 0;
while (theloop < 1) {
do {
cout << "How many North/South Fence posts? ";
cin >> nsfp;
if (nsfp < 2)
cout << "Value must be at least 2, please try againn";
else if (nsfp > 10)
nsfp = 10;
} while (nsfp < 2);
do {
cout << "How many East/West Fence posts? ";
cin >> ewfp;
if (ewfp < 2)
cout << "Value must be at least 2, please try againn";
else if (ewfp > 10)
nsfp = 10;
} while (ewfp < 2);
for (count = 1; count <= nsfp; count++) {
cout << "|";
nsfp2 = nsfp - 1;
for (count = 1; count <= nsfp2; count++) {
cout << "==|";
nsfpw = count;
}
}
cout << "n";
string spaces(3 * nsfp - 4, ' ');
for (count = 1; count < ewfp; count++) {
cout << ":" << spaces << ":n";
cout << "-" << spaces << "-n";
}
for (count = 1; count <= nsfp; count++) {
cout << "|";
nsfp2 = nsfp - 1;
for (count = 1; count <= nsfp2; count++) {
cout << "==|";
nsfpw = count;
}
}
cout << "n" << nsfp << endl;
cout << ewfp;
theloop = 1;
}
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 格式化说明符以可移植地打印 C++11 中的 std::uint64_t 变量
- 无法格式化PWSTR变量中的数据以用于CopyFileW命令
- 我如何在同一行输入2个不同的变量,同时使用setw进行格式化
- 基于其他变量格式化字符串
- 在c++中对齐(格式化)输出//多个变量在一行中
- C++输出格式化变量参数列表的最佳方法