0

I made a very simple program but even though there is not a semicolon, I still get this error. Please ignore the weird intention of this program, it's for comedy purposes.

#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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 5
    Your statement “blocks” are missing braces. – Dave Newton May 10 '22 at 17:37
  • 4
    `if ( ... ) { ... } else { ... }` then you should be golden – AndersK May 10 '22 at 17:38
  • 3
    C++ is not python. You need to surround everything between `if()` and `else` in `{}` unless it's a single statement (and even in that case I recommend adding the `{}`). – fabian May 10 '22 at 17:38
  • 5
    It is tangential, but I would also question the validity of `if (john, jeff, philip, joe, dave)`. – n0rd May 10 '22 at 17:38
  • 1
    Remember that white space like indentation does not have any functional impact on the code, beyond separating tokens. Only the expression immediately following the `if` is considered as part of the conditional branch, which is why `{}` are used when you want to do more than one thing in that branch. – François Andrieux May 10 '22 at 17:39
  • 4
    `cin >> john, jeff, philip, joe, dave;` is going to expect the user to provide a single number, store is in `john`. The comma operator doesn't do what you expect, it just evaluates each operand and results in the last operand. A trial-and-error approach is not feasible for learning C++. Consider getting a reputable book. – François Andrieux May 10 '22 at 17:41
  • As mentioned by n0rd `if (john, jeff, philip, joe, dave)` probably doesn't do what you expect it to do: this evaluates `john`, `jeff`, `philip` and `joe` and yields `dave` resulting in the if having the same effect as `if(dave != 0)` in this scenario. Turn on compiler warnings to detect this accidentally occuring; e.g. gcc would warn you about the dropped values, if you compile with the `-Wall` flag – fabian May 10 '22 at 17:43
  • Handy reading: [How does the Comma Operator work](https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work) Gory details: [Built-in comma operator](https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator) – user4581301 May 10 '22 at 17:44

3 Answers3

1

this code

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");

is actually

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");

indentation has no meaning for c++, you need

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");

it also highly unlikel that the cin and if do what you want. I suspect you are trying to test if somebody's name is John or Jeff etc

in that case you need

    string name;
    cin >> name;

then

    if(name=="Jeff"||name = "John||name ==......)
pm100
  • 48,078
  • 23
  • 82
  • 145
  • I'd also point out that `if (john, jeff, philip, joe, dave)` and `cin >> john, jeff, philip, joe, dave;` are definitely not going to do what they intended to do. – François Andrieux May 10 '22 at 17:42
1

Since you didn't use {} around the body of your if code the compiler reads your code as if it looks like the following:

#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;
}

You should do the following:

#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;
}
James Adkison
  • 9,412
  • 2
  • 29
  • 43
1

For starters in this expression statement

cin >> john, jeff, philip, joe, dave;

there is used the comma operator. It is equivalent to

( cin >> john ), ( jeff ), ( philip ), ( joe ), ( dave );

So all the operands after the first operand

( jeff ), ( philip ), ( joe ), ( dave )

do not produce any effect.

It seems you mean

cin >> john >> jeff >> philip >> joe >> dave;

Again in the condition of this if statement

if (john, jeff, philip, joe, dave)

there is used an expression with the same comma operator. The value of the expression is the value of last operand dave contextually converted to the type bool.

It is unclear what you are trying to check in this if statement.

Nevertheless the following pair of statements should be enclosed in a compound statement like

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;
}

It seems you mean something like the following

#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;
}

The condition in the if statement can be changed as you like.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335