I am new

Hi I am just learning C++ and was doing good until reached this and am unable to find a problem.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
}

When i try and run this it says " 'x': undeclared identifier "
How can i identify 'x' to fix the problem?
You need to initialize a value for x in some fashion.

This is one way to do it

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    int x = 100;

    if (x == 100)
        cout << "x is 100";
    else
        cout << "x is not 100";
}


This would be another way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int x;
    cout << "Please enter a value for x: ";
    cin >> x;
    if (x == 100)
        cout << "x is 100";
    else
        cout << "x is not 100";
}
you have to declare x before if
Thank you Garion!
and this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
    int x=0;
    cout<<"Enter a number: ";
    cin>>x;
    if (x == 100)
        cout << "x is "<<x;
    else
        cout << "x is "<<x<<" not 100";
    return 0;
}

Topic archived. No new replies allowed.