2017-09-04 107 views
-1
#include <iostream> 
#include <cassert> 
using namespace std; 

//constants: 
const int MIN_HEIGHT = 3; 
const int MIN_WIDTH = 3; 
const int MAX_HEIGHT = 20; 
const int MAX_WIDTH = 60; 
const char H = 'h'; 
const char W = 'w'; 
const char B = 'b'; 
const char F = 'f'; 
const char Q = 'q'; 

//prototypes: 
void drawRectangle(int row, int col, char border, char fill); 
void displayChoices(); 
char getChoice(char h, char w, char b, char f, char q); 

int main() 
{ 

    char border, fill, choice; 

    cout << endl << "Welcome! \n"; 
    drawRectangle (10,10, '#','*'); 

    cout << endl << "Choose from the following: \n"; 
    displayChoices(); 
    getChoice(H, W, B, F, Q); 
    //drawRectangle();//not sure how to get the changed value from getChoice 
    //if height is changed, update height. If width is changed, update width of rect. 
    //if border is changed, update rectangle border. If fill is changed, update rectangle fill 

} 

//draws rectangle 
void drawRectangle(int row, int col, char border, char fill) 
{ 
    for (int i = 0; i < row; i++) { 
     for (int j = 0; j < col; j++) { 
      if ((i == 0) || (i == (row - 1)) || (j == 0) || (j == (col - 1))) { 
       cout << border; 
      } 
      else { 
       cout << fill; 
      } 
     } 
     cout << endl; 
    } 
} 

//diplays users choices between height, width 
//border and quit 
void displayChoices() 
{ 
    cout << "h) Change the height.\n"; 
    cout << "w) Change the width.\n"; 
    cout << "b) Change the border character.\n"; 
    cout << "f) Change the fill character.\n"; 
    cout << "q) Quit program.\n"; 
} 

//takes users choice and asks for the new value or character 
char getChoice(char h, char w, char b, char f, char q) { 
    char choice, newBorder, newFill; 
    int newHeight, newWidth, count = 0; 

    cin >> choice; 

    while ((choice != h && choice != w && choice != b && choice != f && choice != q)) { 
     cout << "Not a valid choice. Choose again.\n"; 
     cin >> choice; 
    } 

     if (choice == q) 
      return 0; 

     else if (choice == h) { 
      cout << "Enter new height between " << MIN_HEIGHT << " and " << MAX_HEIGHT << ": \n"; 
      cin >> newHeight; 
      while ((newHeight < MIN_HEIGHT) || (newHeight > MAX_HEIGHT)) { 

       cout << "That number is not in range. Try again.\n"; 
       cout << "Enter a number between " << MIN_HEIGHT 
        << " and " << MAX_HEIGHT << ": "; 
       cin >> newHeight; 

      } return newHeight; 
     } 
     else if (choice == w) { 
      cout << "Enter new width between " << MIN_WIDTH << " and " << MAX_WIDTH << ": \n"; 
      cin >> newWidth; 
      while ((newWidth < MIN_WIDTH) || (newWidth > MAX_WIDTH)) { 

       cout << "That number is not in range. Try again.\n"; 
       cout << "Enter a number between " << MIN_WIDTH 
        << " and " << MAX_WIDTH << ": "; 
       cin >> newWidth; 

      } return newWidth; 
     } 
     else if (choice == b) { 
      cout << "Enter new border character: \n"; 
      cin >> newBorder; 
      return newBorder; 
     } 
     else if (choice == f) { 
      cout << "Enter new fill character: \n"; 
      cin >> newFill; 
      return newFill; 

     } 

    } 

我試圖創建一個應用程序來繪製一個矩形,其中的高度,寬度,邊框和填充矩形的函數內的值可以由用戶來改變(一次一個)。我無法理解如何訪問/保存用戶的輸入(更改爲矩形)並使用更新的值繪製新的矩形。我現在只是在學習C++,所以我的代碼甚至可能不是解決這個問題的正確方法。任何幫助或指導表示讚賞。如何訪問從主(C++)

+1

'const char H ='h';'就像是寫'const int one_hundred_seventeen = 117;'(有點奇怪) –

+1

聽起來像是你可以從閱讀[這些C++書籍]之一中獲益(https:// stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – Ron

回答

0

我肯定會首先建議你嘗試一些較小的程序,如果你是新的C++定義函數。

你定義爲函數getChoice(char, char, char, char, char)返回類型的類型是char,但在功能(除newBorder)所有的返回值是int類型,這是不合適的。

實際上,從函數名稱「getChoice」判斷,該函數應該只返回用戶的「選擇」而不是繼續,並且應該在其他地方進行進一步的指示。

當用戶輸入「Q」,則返回0聲明getChoice不會導致主要功能退出而僅僅導致getChoice函數返回值爲0

int值我也考慮開關在判斷用戶的選擇時,比所有的if-else語句更好。

在我根據您的程序編寫的以下代碼中,將在主函數內部執行進一步的指令。

#include <iostream> 
#include <cassert> 
using namespace std; 

//constants: 
const int MIN_HEIGHT = 3; 
const int MIN_WIDTH = 3; 
const int MAX_HEIGHT = 20; 
const int MAX_WIDTH = 60; 
const char H = 'h'; 
const char W = 'w'; 
const char B = 'b'; 
const char F = 'f'; 
const char Q = 'q'; 

//prototypes: 
void drawRectangle(int row, int col, char border, char fill); 
void displayChoices(); 
char getChoice(char h, char w, char b, char f, char q); 

int main() 
{ 

    char border = '#', fill = '*', choice; 
    int newHeight = 0; 
    int newWidth = 0; 
    char newBorder = (char)0; 
    char newFill = (char)0; 
    int height = 10, width = 10; 
    cout << endl << "Welcome! \n"; 
    while (true) 
    { 
     drawRectangle(height, width, border, fill); 

     cout << endl << "Choose from the following: \n"; 
     displayChoices(); 
     choice = getChoice(H, W, B, F, Q); 
     //if height is changed, update height. If width is changed, update width of rect. 
     //if border is changed, update rectangle border. If fill is changed, update rectangle fill 
     switch (choice) 
     { 
     case Q: 
       return 0; 

     case H: 
       cout << "Enter new height between " << MIN_HEIGHT << " and " << MAX_HEIGHT << ": \n"; 
       cin >> newHeight; 
       while ((newHeight < MIN_HEIGHT) || (newHeight > MAX_HEIGHT)) { 

        cout << "That number is not in range. Try again.\n"; 
        cout << "Enter a number between " << MIN_HEIGHT 
         << " and " << MAX_HEIGHT << ": "; 
        cin >> newHeight; 

       } 
       height = newHeight; 
       break; 

     case W: 
       cout << "Enter new width between " << MIN_WIDTH << " and " << MAX_WIDTH << ": \n"; 
       cin >> newWidth; 
       while ((newWidth < MIN_WIDTH) || (newWidth > MAX_WIDTH)) { 

        cout << "That number is not in range. Try again.\n"; 
        cout << "Enter a number between " << MIN_WIDTH 
         << " and " << MAX_WIDTH << ": "; 
        cin >> newWidth; 

       } 
       width = newWidth; 
       break; 
     case B: 
       cout << "Enter new border character: \n"; 
       cin >> newBorder; 
       border = newBorder; 
       break; 
     case F: 
       cout << "Enter new fill character: \n"; 
       cin >> newFill; 
       fill = newFill; 
       break; 
     } 
    } 
    return 0; 
} 

//draws rectangle 
void drawRectangle(int row, int col, char border, char fill) 
{ 
    for (int i = 0; i < row; i++) { 
     for (int j = 0; j < col; j++) { 
      if ((i == 0) || (i == (row - 1)) || (j == 0) || (j == (col - 1))) { 
       cout << border; 
      } 
      else { 
       cout << fill; 
      } 
     } 
     cout << endl; 
    } 
} 

//diplays users choices between height, width 
//border and quit 
void displayChoices() 
{ 
    cout << "h) Change the height.\n"; 
    cout << "w) Change the width.\n"; 
    cout << "b) Change the border character.\n"; 
    cout << "f) Change the fill character.\n"; 
    cout << "q) Quit program.\n"; 
} 

//takes users choice and asks for the new value or character 
char getChoice(char h, char w, char b, char f, char q) { 
    char choice, newBorder, newFill; 
    int newHeight, newWidth, count = 0; 

    cin >> choice; 

    while ((choice != h && choice != w && choice != b && choice != f && choice != q)) { 
     cout << "Not a valid choice. Choose again.\n"; 
     cin >> choice; 
    } 
    return choice; 


} 

這其實也是一個不好的做法,使用「使用命名空間std;」聲明,但它可以爲初學者減少很多麻煩,所以你現在不需要關心它。只要記住你應該在一段時間後襬脫它。