You are viewing our Forum Archives. To view or take place in current topics click here.
[C++] else statement needs a previous if (help)
Posted:

[C++] else statement needs a previous if (help)Posted:

TTG-HyPer
  • TTG Master
Status: Offline
Joined: Jun 12, 201113Year Member
Posts: 843
Reputation Power: 36
Status: Offline
Joined: Jun 12, 201113Year Member
Posts: 843
Reputation Power: 36
i was trying to practice if and else statements and when it comes to building and running the program the compiler say that the else statement needs a previous if statement. Yet clearly there is an if statement before the else statement. What did i do wrong?

here is the code i'm having trouble with

#include <iostream>

using namespace std;

int main()
{
    float x;

    cout << "Think of any number above zero\n";
    cout << "Add 5 \n";
    cout << "multiply it by 2 \n";
    cout << "then add 6 \n";
    cout << "What is your answer? ";

    cin >> x;
    cin.ignore();
    cout << endl;

    x = x - 6;
    x = x / 2;
    x = x - 5;

    if (x < 0);
    {
        cout << "I said think of a number above zero! \n";
        cout << x << " is not above zero!" << endl;
    }

    else;
    {
        cout << "The number that you was thinking of is " << x << "I cannot be beaten!";
    }
}
#2. Posted:
Experiment5X
  • TTG Senior
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65

else;


Remove the semi-colon after the 'else' keyword
#3. Posted:
TTG-HyPer
  • TTG Master
Status: Offline
Joined: Jun 12, 201113Year Member
Posts: 843
Reputation Power: 36
Status: Offline
Joined: Jun 12, 201113Year Member
Posts: 843
Reputation Power: 36
Experiment5X wrote

else;


Remove the semi-colon after the 'else' keyword


thanks, i'm not very good with the placement of semi-colons yet i started yesterday so i still need to get the hang of it
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.