0

Just trying out my code for fun but when asked about my age and I typed no then it skipped a line..how do I make it work?

#include<iostream> using namespace std; int main() { char name [6]; int num = 1; char no ; cout <<"Hi.My name is Anna "<< endl; cout <<"Whats yours \?"; cout << "\n"; cin >> name ; if (name) { cout << "Oh nice to meet you\n " << name << endl; } else if (no) { cout << "How rude...what an idiot"; } cout << "May i know your age \n" << endl; cin >> num ; cout << "\n"; if (no) { cout << "You cant talk to me like that" << endl; } cout << "\n"; if (num > 10) { cout << "You should focus on studies,since youre very young" << endl; } if (num > 20) { cout << "Wow you grew up so fast boy" << endl; } if (num > 30) { cout << "You were just a kid when i first saw you " << endl; } cout << "\n"; cout <<"Pardon me but i have to go somewhere but great seeing you again"; }

8th Feb 2022, 6:59 AM
Kelzang Thinley
Kelzang Thinley - avatar
6 Respuestas
+ 1
num is of int type so you need to give input of int type only.. Your 'no' variable is uninitialized. So assign a value, What actually you are expecting from this code..? What is your expected outout for a sample input?
8th Feb 2022, 8:45 AM
Jayakrishna 🇮🇳
+ 1
G'day Kelzang Thinley that is some nice looking code. It is tricky to copy/paste into Q&A without losing your indentation. You can add your "code bits" in using the plus icon in these comments/answers. I think it is your if statements aren't checking a variable against a value, I'll have a go & see what happens.
8th Feb 2022, 10:28 AM
HungryTradie
HungryTradie - avatar
+ 1
https://code.sololearn.com/csAlKpxFgFCE/?ref=app I put some comments on my changes, I hope you see what I've done & why. Sorry, I couldn't get the string to check how you wanted it.
8th Feb 2022, 11:13 AM
HungryTradie
HungryTradie - avatar
+ 1
Manav Roy I was trying other ways to use variable in the If statement instead of boolean and it didnt work out confused me instead so had to ask and also there is lots of bugs and use of unessary line but i am new to this and it happens
8th Feb 2022, 7:15 PM
Kelzang Thinley
Kelzang Thinley - avatar