say I have:
int lol;
cout << "enter a number(int): ";
cin >> lol
cout << lol;
If I type 5 then it’ll cout 5. If I type fd it couts some numbers.
How can I specify the value, like say I only want it an int?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you type in
fdit will output some numbers because those numbers are whatlolhappens to have in them before it gets assigned to. Thecin >> loldoesn’t write tololbecause it has no acceptable input to put in it, so it just leaves it alone and the value is whatever it was before the call. Then you output it (which is UB).If you want to make sure that the user entered something acceptable, you can wrap the
>>in anif:Also you might want to assign to
lolbefore reading it in so that if the read fails, it still has some acceptable value (and is not UB to use):If, for example, you want to loop until the user gives you some valid input, you can do