Asigning "x" a value

I've just started trying to learn C++ and going through some tutorials I tried to write a few lines to assign a value to the variable x. It builds with no errors but when I run it it comes up with x and not 5. Some help (without laughing at me too much) would be greatly appreciated.

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

using namespace std;

int main()
{
    int x;
    x = 5;

    cout << "x" << endl;
    return 0;
}
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
 #include <iostream>

using namespace std;

int main()
{
    int x;
    x = 5;

    cout << "x = " << x << endl;
    return 0;
}
closed account (EwCjE3v7)
Welcome CodeWizKid, your problem is, that when you do cout << "x" << endl;, it out puts
x
. Not the value of your int which is x. To fix this you will have to remove the quotation marks, so we output x and not a string literals: cout << x << endl;, output will be
5
.
Last edited on
Thank you very much!
Topic archived. No new replies allowed.