while 循环中的 if/else 格式

发布于 2024-09-28 19:35:03 字数 563 浏览 8 评论 0原文

while(true)
{ 
  cout << "Name: ";
  getline(cin, Name);
  if(Name == "Stop")
    break;

  cout << "Additional Name - Y/N: ";
  getline(cin, additional);
  if (additional == "Y") 
    cout << "Additional Name: ";
  getline(cin, Name2);
  else
    cout << "Location: ";
  getline(cin, Location);
  if(Location == "Stop")
    break;
}


chief.cpp: In member function ‘void BookList::Fill()’:
chief.cpp:128: error: ‘else’ without a previous ‘if’

用户输入第一个名字后,我想提供输入第二个名字的选项。如果“N”则跳至“位置”,如果“Y”则转到“名称2”,然后跳至“位置”。

while(true)
{ 
  cout << "Name: ";
  getline(cin, Name);
  if(Name == "Stop")
    break;

  cout << "Additional Name - Y/N: ";
  getline(cin, additional);
  if (additional == "Y") 
    cout << "Additional Name: ";
  getline(cin, Name2);
  else
    cout << "Location: ";
  getline(cin, Location);
  if(Location == "Stop")
    break;
}


chief.cpp: In member function ‘void BookList::Fill()’:
chief.cpp:128: error: ‘else’ without a previous ‘if’

After the user enters the first name, I would like to give the option to enter a second name. If "N" just skip down to Location, if "Y" go to Name2 and then on to Location.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱殇璃 2024-10-05 19:35:03

您必须将 ifelse 之间的语句括在方括号 { ... } 内。

You have to enclose the statements between the if and the else within brackets { ... }.

二手情话 2024-10-05 19:35:03

您需要在此处添加大括号:

if (additional == "Y") 
     cout << "Additional Name: ";
      getline(cin, Name2);
else
     cout << "Location: ";
     getline(cin, Location);

尝试:

if (additional == "Y") 
{
     cout << "Additional Name: ";
     getline(cin, Name2);
}
// You could have an else here, but that would make location only happen if "additional" was not "Y"

 cout << "Location: ";
 getline(cin, Location);

我怀疑您总是希望出现以下几行(获取位置),因此在这种情况下,不需要其他内容。

You need to add braces here:

if (additional == "Y") 
     cout << "Additional Name: ";
      getline(cin, Name2);
else
     cout << "Location: ";
     getline(cin, Location);

Try:

if (additional == "Y") 
{
     cout << "Additional Name: ";
     getline(cin, Name2);
}
// You could have an else here, but that would make location only happen if "additional" was not "Y"

 cout << "Location: ";
 getline(cin, Location);

I suspect that you always want the following lines (getting location) to occur, so in this case, no else is required.

彼岸花ソ最美的依靠 2024-10-05 19:35:03

您需要将 if 的主体括在大括号中以消除错误。从外观上看,您还需要将 else 主体括在大括号中:

if (additional == "Y") {
  cout << "Additional Name: ";
  getline(cin, Name2);
 } else {
  cout << "Location: ";
  getline(cin, Location);
 }

You need to enclose the body of if in braces to get rid of the error. By the looks of it you also need to enclose the else body in braces:

if (additional == "Y") {
  cout << "Additional Name: ";
  getline(cin, Name2);
 } else {
  cout << "Location: ";
  getline(cin, Location);
 }
酒几许 2024-10-05 19:35:03

仅当块仅包含一个语句(以分号 (; 结尾的内容))时,才可以丢弃块周围的大括号({}) ) 或另一个带有块的语句(ifforwhile 等)。

例如,您可以这样做:

while (true)
    if (foo) {
        printf("Hello, ");
        printf("world\n");
    }

甚至

while (true)
    if (foo)
        for (i = 0; i < 10; i++) {
            printf("Hello, ");
            printf("World!\n");
        }

但是在您的代码中,您需要使用大括号,因为您的 if 语句在 else 语句之前包含两个语句。因此,if 语句在 cout 行之后结束。

最终结果应该是这样的:

if (additional == "Y") {
   cout << "Additional Name: ";
   getline(cin, Name2);
}
else {
   cout << "Location: ";
   getline(cin, Location);
}

You may only discard the braces ({ and }) around blocks when the block contains exactly one statement (something ending with a semicolon (;)) or another statement with a block (if, for, while, etc).

For example, you can do this:

while (true)
    if (foo) {
        printf("Hello, ");
        printf("world\n");
    }

or even

while (true)
    if (foo)
        for (i = 0; i < 10; i++) {
            printf("Hello, ");
            printf("World!\n");
        }

But in your code, you need to use braces since your if statement contains two statements before the else statement. Therefore the if statement end after the cout line.

The end result should thus look something like this:

if (additional == "Y") {
   cout << "Additional Name: ";
   getline(cin, Name2);
}
else {
   cout << "Location: ";
   getline(cin, Location);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文