+ 3
Please why is this code giving me 'adult' as an answer?
10 Respostas
+ 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
+ 2
thanks Saga, nice explanation
+ 1
wow thanks. I found out the default value is higher than 18
+ 1
please what do you mean by initializing variables with default values
+ 1
default value of auto data member is garbaze blue and it may any value.
so your result is undefined here.
+ 1
Sir Saga, I don't get... please what is garbaze blue. I just google it and didn't find any Informatics
+ 1
it means randomly any value(unpridictible value)..
+ 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;
}
+ 1
int age =4;
use this code to initialize the variable.
0 is a better that undefined.
+ 1
@Sammy wel come