+ 3

Please why is this code giving me 'adult' as an answer?

https://code.sololearn.com/coTmjhtC91NZ/?ref=app

15th Dec 2017, 9:32 PM
Sammy
Sammy - avatar
10 odpowiedzi
+ 3
c++ is not initializing its variables with default value, so after age is created it contains a random number (some say it is the value which was left on that memory position) try add cout << age; to your code and see what is the real value of age It is a good habbit to initialize all variables with default values. please add c++ in the tags of your question
15th Dec 2017, 9:45 PM
sneeze
sneeze - avatar
+ 2
thanks Saga, nice explanation
16th Dec 2017, 7:56 AM
Sammy
Sammy - avatar
+ 1
wow thanks. I found out the default value is higher than 18
16th Dec 2017, 12:25 AM
Sammy
Sammy - avatar
+ 1
please what do you mean by initializing variables with default values
16th Dec 2017, 12:27 AM
Sammy
Sammy - avatar
+ 1
default value of auto data member is garbaze blue and it may any value. so your result is undefined here.
16th Dec 2017, 2:17 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
Sir Saga, I don't get... please what is garbaze blue. I just google it and didn't find any Informatics
16th Dec 2017, 6:35 AM
Sammy
Sammy - avatar
+ 1
it means randomly any value(unpridictible value)..
16th Dec 2017, 6:38 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
hey you can confirm that ramdom value by cout age variable ...like below#include <iostream> using namespace std; int main() { int age; cout << age <<endl; if (age > 14) { if(age >= 18) { cout << "Adult"; } else { cout << "Teenager"; } } else { if (age > 0) { cout << "Child"; } else { cout << "Something's wrong"; } } return 0; }
16th Dec 2017, 7:06 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
int age =4; use this code to initialize the variable. 0 is a better that undefined.
16th Dec 2017, 8:10 AM
sneeze
sneeze - avatar
+ 1
@Sammy wel come
16th Dec 2017, 10:32 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar