0
How can I check if a user didn't input anything
C++ empty input
6 Answers
+ 3
if (input == "") // error handling
+ 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
+ 1
Take a look at isBlank() and String.empty() functions
0
string input="";
cout<<"enter value" <<endl;
cin>>input;
if(input.length()==0){
cout<<"empty entry"<<endl;
}
else{
cout<<"have a value"<<endl;
}
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?
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.