Error: 'else' without a previous "if' even though there is not a semicolon

112 Views Asked by At

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

There are 3 best solutions below

0
On BEST ANSWER

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.

1
On

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 ==......)
0
On

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