2017-09-04 1 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; 

     } 

    } 

Ich versuche, eine Anwendung zu erstellen, ein Rechteck, in dem die Höhe, Breite, Grenze zu ziehen, und das Rechtecks ​​ausfüllen kann vom Anwender geändert werden (eins nach dem anderen). Ich habe Probleme zu verstehen, wie ich auf die Benutzereingabe zugreifen/sie speichern kann (ändere das Rechteck) und zeichne das neue Rechteck mit dem aktualisierten Wert. Ich lerne gerade C++, also ist mein Code möglicherweise nicht der richtige Weg, um dieses Problem anzugehen. Jede Hilfe oder Anleitung wird geschätzt.Wie Werte innerhalb von Funktionen aus dem Haupt (C++) für den Zugriff auf

+1

'const char H = 'h';' ist wie schreiben 'const int one_hundertredenteen = 117;' (es ist ein bisschen komisch) –

+1

Klingt, als könnten Sie davon profitieren, eines dieser [C++ Bücher] zu lesen (https: // stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron

Antwort

0

Ich würde Ihnen sicherlich zuerst raten, einige kleinere Programme auszuprobieren, wenn Sie die Funktionen in C++ neu definieren.

Der Typ, den Sie als Rückgabetyp der Funktion definiert getChoice(char, char, char, char, char) ist char, aber alle Rückgabewerte in der Funktion (außer newBorder) vom Typ int, die nicht geeignet ist.

In der Tat sollte die Funktion nach dem Namen Ihrer Funktion "getChoice" nur die "Wahl" des Benutzers zurückgeben, anstatt weiter zu gehen, und weitere Anweisungen sollten an anderer Stelle erfolgen.

Wenn der Benutzer eingibt ‚q‘, die Hauptfunktion zu beenden, sondern nur dazu führen, die getChoice Funktion einen int-Wert von 0.

Ich halte auch einen Schalter zurück die Rückkehr 0 Anweisung in getChoice nicht verursachen würde -case-Anweisung besser als alle if-else-Anweisungen, wenn die Entscheidung des Benutzers beurteilt wird.

Im folgenden Code, den ich basierend auf Ihrem Programm geschrieben habe, werden weitere Anweisungen innerhalb der Hauptfunktion ausgeführt.

#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; 


} 

Es ist auch eine schlechte Praxis, die 'using namespace std;' Aussage, aber es kann eine Menge Ärger für Anfänger reduzieren, so dass Sie sich jetzt nicht darum kümmern müssen. Denken Sie daran, Sie sollten es nach einiger Zeit loswerden.

Verwandte Themen