We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
//mrityunjay38@gmail.com#include<bits/stdc++.h>usingnamespacestd;classPerson{public:intage;//constructorPerson(intinitialAge){boolvalidAge=check(initialAge);validAge==true?age=initialAge:age=0,cout<<"Age is not valid, setting age to 0.\n";};//check age validityboolcheck(intage){if(age<0)returnfalse;returntrue;}voidamIOld(){if(age<13){cout<<"You are young.\n";return;}if(age>=13andage<18){cout<<"You are a teenager.\n";return;}cout<<"You are old.\n";}voidyearPasses(){age++;}};intmain(){intt;intage;cin>>t;for(inti=0;i<t;i++){cin>>age;Personp(age);p.amIOld();for(intj=0;j<3;j++){p.yearPasses();}p.amIOld();cout<<'\n';}return0;}
Technically, it is giving the right answer but the expected output doesn't repeat this -> "Age is not valid, setting age to 0." Thought it should get printed on each test case run but in the questions the expected output skips this lines after 1st run. Why?
Class vs. Instance
You are viewing a single comment's thread. Return to all comments →
What is wrong with my code?
Technically, it is giving the right answer but the expected output doesn't repeat this -> "Age is not valid, setting age to 0." Thought it should get printed on each test case run but in the questions the expected output skips this lines after 1st run. Why?