为课程分配工作,要求我们提供输入,以便我们创建一个"畜栏"基于输入的数字。我真的很难在基于另一个变量尝试获得正确的间距。畜栏应该看起来像:
|==|==|==|
: :
- -
: :
|==|==|==|
我的问题是中心的空白区域(应该基于顶部/底部的长度)。 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 again\n";
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 again\n";
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;
}
}
答案 0 :(得分:0)
也许您正在寻找的是这个。空格的数量是
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 again\n";
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 again\n";
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 again\n";
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 again\n";
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;
}
}