0

How can I check if a user didn't input anything

C++ empty input

29th May 2020, 8:07 PM
The Unknown
The Unknown - avatar
6 Answers
+ 3
if (input == "") // error handling
29th May 2020, 8:12 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Give an example to help u I mean i wanna know what type of value which should be entered there are two solutions first : usinf the function length() about but it in if condtion second : using if condtion only
29th May 2020, 8:13 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 1
Take a look at isBlank() and String.empty() functions
29th May 2020, 8:26 PM
HNNX 🐿
HNNX 🐿 - avatar
0
string input=""; cout<<"enter value" <<endl; cin>>input; if(input.length()==0){ cout<<"empty entry"<<endl; } else{ cout<<"have a value"<<endl; }
29th May 2020, 8:21 PM
Luis E. GĂłmez Mena
Luis E. GĂłmez Mena - avatar
0
The examples that you gave are only strings. I mean every input from any data type. How am I going to check if the user input or not ? #include <iostream> using namespace std; int main(){ int num; cout<<"Enter a number:"; cin>>num; How can I check if the user didn't input the number. How can I do that with any data type. What's the condition?
29th May 2020, 8:36 PM
The Unknown
The Unknown - avatar
0
In reply to your last post...here's an example..... #include <iostream> #include <string> #include <cctype> using namespace std; int main(){ string input; int myint; cout << "Enter a number:"; cin >> input; if(isdigit(input[0])){ myint = atoi(input.c_str()); cout << "Number entered"; }else{ cout << "Error.....Input was not a number"; } return 0; } ...with regards to your initial question...the examples in the other post will not work i.e input.length() using just "cin", try them out to see. If you still need something similar...let me know.
29th May 2020, 9:10 PM
rodwynnejones
rodwynnejones - avatar