我做了一个非常简单的程序,但即使没有分号,我仍然得到这个错误。请忽略这个节目的奇怪意图,它是为了喜剧目的。
#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语句中的条件可以随意修改。
由于您没有在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;
}
此代码
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 ==......)