1. 程式人生 > >為什麽我的switch語句不正確輸出星號在我的c++程序?

為什麽我的switch語句不正確輸出星號在我的c++程序?

switch lse 為什麽 應該 選擇 謝謝 out 使用 leg

程序應該輸出可變長度的垂直和水平線使用星號。 星號和方向的數量將會是決定由用戶輸入的語句。 它必須使用switch語句創建。 這是我目前的代碼:

int main() {

// Variables
int length = 1;
char direct;

// User input choice
if (length >= 1 && length <= 20) {
    cout << "\nEnter the line length and direction: ";
    cin >> length >> direct;
}

// If user input incorrect
else {
    system("pause");
}

// Switch cases for horizontal or vertical
switch (direct) {
case ‘h‘: for (int count = 0; count <= length; count++) {
    cout << "*";
    break;
}
case ‘H‘: for (int count = 0; count <= length; count++) {
    cout << "*";
    break;
}
case ‘V‘: for (int count = 0; count <= length; count++) {
    cout << "*" << "\n" << endl;
    break;
}
case ‘v‘: for (int count = 0; count <= length; count++) {
    cout << "*" << "\n" << endl;
    break;
}

default:  cout << "Illegal comand" << endl;

}

system("pasue");

}
這就是我的一個水平選擇輸出語句看起來像:

Enter the line length and direction: 4 h


*

Illegal Command
這就是我的一個垂直的選擇輸出語句看起來像:

Enter the line length and direction: 4 v

*

Illegal Command
這是我想要的水平一個看起來像:

Enter the line length and direction: 4 h


這是我想要的垂直一個看起來像:

Enter the line length and direction: 4 v





為什麽星號不輸出正確嗎? 為什麽每次輸出“非法命令”嗎? 也認為我應該註意到我是一個初學者在C + +。 謝謝!

為什麽我的switch語句不正確輸出星號在我的c++程序?