【发布时间】: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