holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式

holeMenuProgram.cpp:38:1 Error: Expected Primary-Expression before '}' token

本文关键字:表达式 令牌 错误 cpp holeMenuProgram      更新时间:2023-10-16

我正在使用开关大小写创建一个菜单,但是当我使用 do-while 语句过滤掉不可用的选项时,我遇到了一个奇怪的错误。我能了解一下我在这里做错了什么吗?提前感谢您,因为我是一名新程序员。我知道大小写开关中没有任何内容,但这不应该阻止它正确编译。

//Programmer: Lane Floyd  Date: 2/17/2020
//File: holeMenuProgram.cpp
//Description: 
#include <iostream>
using namespace std;
int main()
{
int menuChoice;  //User input 1-4 for menu choice.
do
{
// Below is the menu.
cout << "       Menu        " << endl;
cout << "       ----        " << endl;
cout << "1.  Enter a number    " << endl;
cout << "2.  Power the number    " << endl;
cout << "3.  Cube root of the number    " <<endl;
cout << "4.  Quit"  <<endl;
cout << "Input Choice: r";
cin >> menuChoice;
} while (menuChoice != 1 || menuChoice != 0);

switch(menuChoice)
{
case 1:
case 2:
case 3:
case 4:
}

return 0;
}

标签必须位于语句之前。你可以写例如

switch(menuChoice)
{
case 1:
case 2:
case 3:
case 4:
; 
}

在 switch 语句的右大括号之前放置一个 null 语句。

或者为了使代码更具可读性,您可以插入至少一个中断语句,例如

switch(menuChoice)
{
case 1:
case 2:
case 3:
case 4:
break; 
}