name spaces

Write your question here.

Hey guys,

I am trying to write the value entered on line 7 to the integer under the name space "example"

1. Is this possible
2. If so what am I doing wrong?

1
2
3
4
5
6
7
8
9
namespace example
{
    int pers;
}
int personal()
{
    cout << "enter a value: " << endl;
    cin >> example::pers;
}


Because correct me if I am wrong but I can call an int from that name space with;

1
2
3
4
5
6
7
8
namespace example
{
    int pers = 5;
}
int personal()
{
    cout << "blah: " << example::5 << endl;
}

blah: 5
The first code must work perfectly, but the second will not work.

Aceix.
@Aceix

Thanks for the reply, am I able to write a value to an integer in a separate namespace like I am trying to do in the 2nd code?
example::5 doesn't make sense. I guess you mean example::pers. If so, then it will work.
@Peter87

yeah sorry that was a typo, but you're right it works, I don't what I was doing earlier but that was my first time giving it a go and I was sure I was doing something wrong.

Thanks guys :)
@OP

To give pers a value, it can be done this way: example::pers=5;

Aceix.
Topic archived. No new replies allowed.