I'm a beginner in C++ and I'm wondering if you can help me.
I'm making a program and error checking is required for this program. So, how can I accept integer only and ignore other data type?
For example:
int tilenumber; cin >> tilenumber; cin.clear(); cin.ignore(); cin >> words; When my code runs:
Input : 1 hey i wanna dance
Output : ey i wanna dance
///
What I want:
Case 1: Input : 1
hey i wanna dance
Output : hey i wanna dance
Case 2: Input : 1e
hey i wanna dance
Output : hey i wanna dance
Why does my code not working?
I tried to solve my problem with my code like above but it's not working.
Thanks.
cin >> tilenumbersetscin'sfailbitstate if the conversion fails. Always check the stream's state after extracting input, eg:if (cin >> tilenumber) { use tilenumber ... } else { error ... }