【问题标题】:Error w/declaration on else statement (C++)else 语句中的错误声明 (C++)
【发布时间】:2015-06-20 03:37:44
【问题描述】:
#include <iostream>

using namespace std;

// Range of numbers, handles input in which the first number is smaller than the second.

int main()
{
    int max = 10;
    int min = 0;
    while(cin >> min)
    {
        if(min<max)
        {
            ++min;
            cout << "The number inputted is well in the RANGE: " << min << endl;
        else
        {
            cout << "The number inputted is not in the RANGE: " << max << endl;
        }
    }
} // End of if.
}

发生了什么事?为什么这不起作用?我是 Stack 的新手,所以我尝试发布此内容,有什么帮助吗?

【问题讨论】:

  • 这怎么行?请更准确。
  • 如果这是您的实际代码,请查看它们没有正确匹配的大括号(现在我已经清理了您的格式)

标签: c++ if-statement declaration


【解决方案1】:

你应该在开始你的 else 部分之前结束 if :

int main()
{
int max = 10;
int min = 0;
while(cin >> min){
   if(min<max){
     ++min;         //dont understand why do you do this !
     cout << "The number inputted is well in the RANGE: " << min << endl;
     } // End of if.
   else{
   cout << "The number inputted is not in the RANGE: " << max << endl;
    }
   }    
}

【讨论】:

  • 感谢它运行良好,我想我忽略了那部分,我认为我们不需要结束 ifs。谢谢。
  • 欢迎您@Seeist
猜你喜欢
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多