cplusplus.com
C++ : Forum : General C++ Programming : Outputting nothing
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


question Outputting nothing

adrianvidal (16)
Is it possible to output a variable with no value? Also, is there a code to know if a variable has a value? I need this concepts. Help. Urgent.
onur (126)
What do you mean by "no value"?
1
2
3
int a ; // i.e. unitialized
int b = 0 ; // i.e. zero
double c = 1.0/0.0 ; // i.e. NaN 


In these cases: Simply test it!

Otherwise you have to bee more specific ;-).

Also, is there a code to know if a variable has a value?

I can tell you this is not possible for any integral type since you cannot tell if a certain value was assigned or is still the initial value. You can however if you know your numbers are always positive you can treat variables containing negative numbers as "having no value".

In other cases you will have to use your own class and mark data if it has been set or is still the initial value.
adrianvidal (16)
I tested outputting a variable with no value. I got a delta character. However, I don't want to have this character.
eker676 (427)
Well I suggest you initialize the variable then. The computer randomly selects something to throw in uninitialized variables.
Return 0 (1282)
I'm curious, what compiler are you using? Attempting to output an uninitialized variable should throw a compile or runtime error.
jsmith (5804)
Perhaps a compile-time warning at best, but definitely not an error.

OP is basically doing

char ch;
cout << ch;

and getting whatever character happened to be on the stack at the address of ch.
Topic archived. No new replies allowed.