【问题标题】:C++ If/else/nesting issuesC++ if/else/嵌套问题
【发布时间】:2010-11-03 20:46:46
【问题描述】:

我正在尝试学习 c++,但我的 if/else 语句遇到了问题。 当我在不打开和关闭大括号的情况下嵌套它们时,我以为我把它们放下了,但我尝试使用大括号,但我得到了错误。

有人能指出什么是错误的以及如何解决它,而不仅仅是答案;那样我什么都学不到。

这是我的来源:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
 signed long int RealNumber = 31710;
 int Guess;

 cout << "Lets see if you can guess my favorite number... \n";
 cout << "Type in a number and hit enter to see if you have guessed it correctly. \n";
 cout << "you should know this one Danielle. \n \n ";
 cin >> Guess;


 if (Guess == RealNumber)
 {
  cout << "Wow, you are amazing! \n";
  cout << "Would you like to be punched?";
 }
 else
 {
  if (Guess < RealNumber)
   cout << "The number is higher";
  else
   if (Guess > RealNumber)
    cout << "The number is lower";
   else
    cout << "That is impossible!"; //trying to make sure that if anthing but a number is entered that the program doesn't crash.
 }

}
 char f;  // used to make the program wait for input before closing. 
 cin >> f;

 return 0;

【问题讨论】:

  • 发布编译器的输出也会很有用。这里一个明显的错误是“char f;”声明之前的右大括号。

标签: c++ nested if-statement


【解决方案1】:

你有一个额外的大括号,上面写着-

char f; 
cin >> f; 

这个右大括号匹配你的主函数的初始大括号。

您在 if/else 嵌套中使用的支撑结构对我来说似乎很好。

这里有一个提示 - 对于每个左大括号,请立即键入右大括号,并为其添加注释。你不必永远使用这个“拐杖”,但作为一个学习编写代码的初学者,这可能会很有帮助。

第 1 步:

int main()
{
}//main 

第 2 步:

int main()
{
    int foo; 
    cin >> foo; 
    if (foo < 100)
    {
        //todo 
    }// if (foo < 100)
}//main

第 3 步:

int main()
{
    int foo; 
    cin >> foo; 
    if (foo < 100)
    {
        cout << "foo is too small";
        cin >> foo; 
        if (foo < 100)
        {
            //todo 
        } // if (foo < 100), inner if statement 
    }// if (foo < 100)
}//main

等等。

【讨论】:

  • 感谢您的提示,我将利用它来防止进一步的简单错误,这也是我注释代码的好方法。
  • 而 kotlinski 关于缩进的评论也很棒。缩进通常可以为不匹配的大括号提供简单的视觉线索。更重要的是,它会阻止您一开始就引入额外的大括号。
  • 一些编辑器(如 Notepad++)也会突出显示它们。非常有用的功能。
  • 右括号上的那些 cmets 会使代码混乱,并且很快就会失去同步。
  • sbi 是对的。那些 cmets 就像训练轮。一旦感觉舒服就停止使用它们。
【解决方案2】:

使用具有自动缩进功能的文本编辑器是个好主意。这可能会帮助您避免像这样的“简单”错误。

【讨论】:

    【解决方案3】:

    请看大卫的回答。我还想指出,通常情况下,您需要编写 if-else 代码,如下所示:

     if (Guess == RealNumber)
     {
       cout << "Wow, you are amazing! \n";
       cout << "Would you like to be punched?";
     }
     else if (Guess < RealNumber)
     {
       cout << "The number is higher";
     }
     else if (Guess > RealNumber)
     {
       cout << "The number is lower";
     }
     else
     {
        cout << "That is impossible!";
     }
    

     if (Guess == RealNumber)
     {
       cout << "Wow, you are amazing! \n";
       cout << "Would you like to be punched?";
     }
     else if (Guess < RealNumber)
       cout << "The number is higher";
     else if (Guess > RealNumber)
       cout << "The number is lower";
     else
        cout << "That is impossible!";
    

    【讨论】:

    • 正确。 IMO 嵌套 if/else 混淆了开发人员正在发生的事情,降低了可读性。在现实生活中,最终会有其他人维护您的代码。
    【解决方案4】:

    试试这个

    #include <iostream>
    using namespace std;
    
    int main()
    {
     signed long int RealNumber = 31710;
     int Guess;
    
     cout << "Lets see if you can guess my favorite number... \n";
     cout << "Type in a number and hit enter to see if you have guessed it correctly. \n";
     cout << "you should know this one Danielle. \n \n ";
     std::cin >> Guess;
    
    
     if (Guess == RealNumber)
     {
      cout << "Wow, you are amazing! \n";
      cout << "Would you like to be punched?";
     }
     else
     {
      if (Guess < RealNumber)
       cout << "The number is higher";
      else
       if (Guess > RealNumber)
        cout << "The number is lower";
       else
        cout << "That is impossible!"; //trying to make sure that if anthing but a number is entered that the program doesn't crash.
     }
    
     char f;  // used to make the program wait for input before closing. 
     std::cin >> f;
    
     return 0;
    } // <-- this bracket was misplaced
    

    您的 main() 括号放错了位置。

    【讨论】:

    • 所以我正确嵌套了 if 和 else 函数(正确调用的是什么)?我将如何做到这一点,以便如果我输入除数字以外的任何内容,它可以输出文本说选择一个数字?
    • 对于一个不同的问题,这是一个不同的答案 :) 但正如您所知,通常认为不在 if-then-else 语句周围使用括号是不好的做法。可读性是关键。
    【解决方案5】:

    你应该使用{},你有if-else链。编译器看不到您如何缩进 if-else(以“数字更高”开头),并将其视为一个扁平的 if-elseif-else。但是,在这种特殊情况下,我认为它不会给您带来问题。

    【讨论】:

      【解决方案6】:

      尝试计算左大括号和右大括号。

      【讨论】:

        【解决方案7】:

        删除#include &lt;stdafx.h&gt;(如果这是 Visual Studio 项目,请在项目设置中关闭“预编译头文件”)。

        将您放置在 main 的结尾 } 大括号之后的语句移到该大括号之前。

        你不能在函数之外有非声明语句。

        干杯&hth。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多