2016-10-04 64 views
0

處理要求我們輸入的類的賦值,以便基於來自輸入的數字創建「畜欄」。基於另一個變量,我正在努力嘗試獲得正確的間距。畜欄應該是這個樣子:基於其他變量格式化字符串

|==|==|==| 
:  : 
-  - 
:  : 
|==|==|==| 

我的問題是在中心(這應該基於頂部/底部的長度)的空白。 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; 
    } 
} 
+3

您應該能夠創建一個間距變量,在您的例子包含8個空格。這樣你就可以在行之間寫出來。如果你不確定如何知道它是8,可以嘗試手動繪製一些欄目,計算和計算數學來找出關係。請注意,由於這是作業,我正在掩蓋你的其他問題並專注於你所問的問題。 – Guvante

+4

提示。如果你需要一個「空格塊」,你總是可以像std :: string space_block(number_of_spaces,'');' – NathanOliver

回答

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; 
    } 
} 
+4

一樣使用'std :: string'這非常接近爲他們做某人的作業! –

+0

我想問你是如何得出有關間距的答案的。這確實是正確的,我只是困惑,你如何到達那裏 –

+0

@ChrisU只需計算空間的數量。每個籬笆除了最後一個以外3個空間。最後一道柵欄的1個空間少於每行字符2個空間。所以3 *(n-1)+ 1 -2 = 3 * n - 4 – user3286661