[Solved] Problem with C++ code - uses random value | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

[Solved] Problem with C++ code - uses random value

I am working on a program that passes in a user's single value if they don't enter a second number (if number == NULL) Otherwise I have it pass in both. My program passes in the randomly generated value even if I pre-determine if the user hasn't entered anything. Do I have to use a pointer and then check for a NULL state? (I come from a higher-level background, so I'm not used to using pointers for operations like this). If so, I think I know what to do. https://code.sololearn.com/cJgfg9yCC14T/?ref=app

19th May 2017, 8:29 PM
Nik
Nik - avatar
1 Antwort
+ 7
The NULL macro should only be used for comparison with pointers, as that's what it's meant for. It's almost always equivalent to the integer 0, so what you're really checking is "dos == 0". "dos", since it is uninitialized, will contain whatever existed in memory at that address before it was used for that variable. C++ does not automatically initialize or zero-out memory before giving it to you. What you could do is simply initialize "dos" to 0 and then check for that, or check cin.fail() to see if it was given an invalid input.
19th May 2017, 9:11 PM
Squidy
Squidy - avatar