處理要求我們輸入的類的賦值,以便基於來自輸入的數字創建「畜欄」。基於另一個變量,我正在努力嘗試獲得正確的間距。畜欄應該是這個樣子:基於其他變量格式化字符串
|==|==|==|
: :
- -
: :
|==|==|==|
我的問題是在中心(這應該基於頂部/底部的長度)的空白。 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;
}
}
您應該能夠創建一個間距變量,在您的例子包含8個空格。這樣你就可以在行之間寫出來。如果你不確定如何知道它是8,可以嘗試手動繪製一些欄目,計算和計算數學來找出關係。請注意,由於這是作業,我正在掩蓋你的其他問題並專注於你所問的問題。 – Guvante
提示。如果你需要一個「空格塊」,你總是可以像std :: string space_block(number_of_spaces,'');' – NathanOliver