Error: 'else' without a previous "if' even though there is not a semicolon
Error: 'else' without a previous "if' even though there is not a semicolon
我做了一个非常简单的程序,但是即使没有分号,我仍然会出现这个错误。请无视本节目的诡异意图,纯属搞笑。
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int john, jeff, philip, joe, dave;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> john, jeff, philip, joe, dave;
if (john, jeff, philip, joe, dave)
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
else
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
system ("pause");
return 0;
}
此代码
if (john, jeff, philip, joe, dave)
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
else
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
system ("pause");
实际上是
if (john, jeff, philip, joe, dave)
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
else
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
system ("pause");
缩进对c++没有意义,你需要
if (john, jeff, philip, joe, dave){
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
} else {
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
system ("pause");
它也不太可能让 cin 和 if 做你想做的事。我怀疑您正在尝试测试某人的名字是 John 还是 Jeff 等等
在那种情况下你需要
string name;
cin >> name;
然后
if(name=="Jeff"||name = "John||name ==......)
由于您没有在 if
代码主体周围使用 {}
,因此编译器会像下面这样读取您的代码:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int john, jeff, philip, joe, dave;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> john, jeff, philip, joe, dave;
if (john, jeff, philip, joe, dave)
{
cout << "you have been cursed, you will have bad luck" << endl;
}
cout << "for the rest of your life!" << endl;
else
{
cout << "you have been blessed, enjoy your life" << endl;
}
cout << "and keep praying to God" << endl;
system ("pause");
return 0;
}
您应该执行以下操作:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int john, jeff, philip, joe, dave;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> john, jeff, philip, joe, dave;
if (john, jeff, philip, joe, dave)
{
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
}
else
{
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
system ("pause");
return 0;
}
此表达式语句中的初学者
cin >> john, jeff, philip, joe, dave;
使用了逗号运算符。相当于
( cin >> john ), ( jeff ), ( philip ), ( joe ), ( dave );
所以第一个操作数之后的所有操作数
( jeff ), ( philip ), ( joe ), ( dave )
不产生任何影响。
你的意思好像是
cin >> john >> jeff >> philip >> joe >> dave;
再次在这个if语句的条件下
if (john, jeff, philip, joe, dave)
使用了具有相同逗号运算符的表达式。表达式的值是最后一个操作数 dave
上下文转换为 bool 类型的值。
不清楚您要在此 if 语句中检查什么。
尽管如此,下面的一对语句应该包含在像
这样的复合语句中
if (john, jeff, philip, joe, dave)
{
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
}
else
{
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
你的意思好像是下面这样
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string name;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> name;
if (name == "john" || name == "jeff" || name == "philip" || name == "joe" || name == "dave")
{
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
}
else
{
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
system ("pause");
return 0;
}
if语句中的条件可以随意更改。
我做了一个非常简单的程序,但是即使没有分号,我仍然会出现这个错误。请无视本节目的诡异意图,纯属搞笑。
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int john, jeff, philip, joe, dave;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> john, jeff, philip, joe, dave;
if (john, jeff, philip, joe, dave)
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
else
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
system ("pause");
return 0;
}
此代码
if (john, jeff, philip, joe, dave)
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
else
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
system ("pause");
实际上是
if (john, jeff, philip, joe, dave)
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
else
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
system ("pause");
缩进对c++没有意义,你需要
if (john, jeff, philip, joe, dave){
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
} else {
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
system ("pause");
它也不太可能让 cin 和 if 做你想做的事。我怀疑您正在尝试测试某人的名字是 John 还是 Jeff 等等
在那种情况下你需要
string name;
cin >> name;
然后
if(name=="Jeff"||name = "John||name ==......)
由于您没有在 if
代码主体周围使用 {}
,因此编译器会像下面这样读取您的代码:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int john, jeff, philip, joe, dave;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> john, jeff, philip, joe, dave;
if (john, jeff, philip, joe, dave)
{
cout << "you have been cursed, you will have bad luck" << endl;
}
cout << "for the rest of your life!" << endl;
else
{
cout << "you have been blessed, enjoy your life" << endl;
}
cout << "and keep praying to God" << endl;
system ("pause");
return 0;
}
您应该执行以下操作:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int john, jeff, philip, joe, dave;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> john, jeff, philip, joe, dave;
if (john, jeff, philip, joe, dave)
{
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
}
else
{
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
system ("pause");
return 0;
}
此表达式语句中的初学者
cin >> john, jeff, philip, joe, dave;
使用了逗号运算符。相当于
( cin >> john ), ( jeff ), ( philip ), ( joe ), ( dave );
所以第一个操作数之后的所有操作数
( jeff ), ( philip ), ( joe ), ( dave )
不产生任何影响。
你的意思好像是
cin >> john >> jeff >> philip >> joe >> dave;
再次在这个if语句的条件下
if (john, jeff, philip, joe, dave)
使用了具有相同逗号运算符的表达式。表达式的值是最后一个操作数 dave
上下文转换为 bool 类型的值。
不清楚您要在此 if 语句中检查什么。
尽管如此,下面的一对语句应该包含在像
这样的复合语句中if (john, jeff, philip, joe, dave)
{
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
}
else
{
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
你的意思好像是下面这样
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string name;
cout << "Hello and welcome to the blessing service" << endl;
cout << "please enter your name and god will" << endl;
cout << "decide if you are cursed or blessed" << endl;
cout << "______________________________________" << endl;
cin >> name;
if (name == "john" || name == "jeff" || name == "philip" || name == "joe" || name == "dave")
{
cout << "you have been cursed, you will have bad luck" << endl;
cout << "for the rest of your life!" << endl;
}
else
{
cout << "you have been blessed, enjoy your life" << endl;
cout << "and keep praying to God" << endl;
}
system ("pause");
return 0;
}
if语句中的条件可以随意更改。