为什么我总是"error: expected unqualified-id before 'case'"?



我正在制作一个代码,用户在其中使用 switch 语句决定他们想要看到的形状。 我在前四个案例中做得很好,但是当我遇到第五个案例时,我遇到了麻烦。 每当我尝试制作第五个案例并且我无法找出问题时,我都会收到一条错误消息,说"错误:'案例'之前的预期不合格 id"。

我的代码发生了什么变化?

    #include <iostream>
using namespace std;
int main() {
int choice;
cout << "Please enter the shape you would like to see" << endl;
cout << "1. Rectangle" << endl;
cout << "2. Square" << endl;
cout << "3. Right Triangle" << endl;
cout << "4. Isosceles Triangle" << endl;
cout << "5. Kite" << endl;
cout << "6. Quit" << endl;
cin >> choice;
switch(choice){
    case 1 : double length,width; //rectangle
             cout << "Enter the length and width of the rectangle" << endl;
             cin >> length >> width;
             for(int i = 0; i < length; i++){
             for(int j = 0; j < width; j++){
             cout << "*";
             }
             cout << "n";
             }
    break;
    case 2 : cout << "Enter the length and width of the square" << endl; 
             //square
             cin >> length >> width;
             for(int i = 0; i < length; i++){
             for(int j = 0; j < width; j++){
             cout << " * ";
             }
             cout << "n";
             }
    break;
    case 3 : cout << "Enter the length of the triangle" << endl;      
             //right triangle
             cin >> length;
             for(int i = 0; i < length; i++){
             for(int j = 0; j < i; j++){
             cout << "*";
             }
             cout << "n";
             }
    break;
    case 4 : int star=1;
             for(int i=1;i<=5;i++){
             for(int j=4;j>=i;j--){
             cout<<" ";
             }
             for(int z=0;z<star;z++){
             cout<<"*";  
             }
             cout<<endl;   
             star=star+2;
             }
             }
             }
    case 5 : cout << "???";

您的大括号不匹配。在case 5之前,您还有两个额外的}

这些牙套应该在case 5之后。一个用于 switch 语句,一个用于main函数。

此外,第一行的#include不应有任何缩进。

最新更新