我正在尝试学习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;
答案 0 :(得分:5)
你的上方有一个额外的支架 -
char f;
cin >> f;
这个右大括号与主要功能的初始大括号相匹配。
你在if / else嵌套中使用的支撑结构对我来说似乎很好。
这是一个提示 - 对于每个左大括号,立即键入右大括号,并为其添加注释。你不必永远使用这个'拐杖',但作为一个学习编写代码的初学者,这个coudl非常有帮助。
第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
等。
答案 1 :(得分:2)
使用带有自动缩进的文本编辑器是个好主意。这可能会帮助您避免像这样的“简单”错误。
答案 2 :(得分:2)
请看大卫的答案。我还想指出,通常情况下,您需要编写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!";
答案 3 :(得分:1)
试试这个
#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()
括号错位了。
答案 4 :(得分:1)
你应该使用{}
,你有if-else链。编译器没有看到你如何缩进if-else(以“数字更高”开头)并且它将它看作if-elseif-else的一个平面。但是,在这种特殊情况下,我没有看到它导致您出现问题。
答案 5 :(得分:-1)
尝试计算开始和结束括号。
答案 6 :(得分:-1)
删除#include <stdafx.h>
(如果这是Visual Studio项目,请在项目设置中关闭“预编译头”)。
在该大括号之前移动您在}
大括号main
后面放置的语句。
您不能在函数外部使用非声明语句。
干杯&amp;第h