[C / C ]Check the input string can be zero in C – Check string import is number in C
There are many checks but here we use the function cin.fail()
#include <iostream>
using namespace std;
int main()
{
double a;
cout<<"nHay nhap vao ki tu a";
cin>>a;
if(cin.fail())
cout<<"n Bi loi roi";
else
cout<<"nSo vua nhap vao la: "<<a;
return 0;
}
Also can be used:
string line;
getline(cin,line);
if (atof(line.c_str()) || line == "0") cout<<endl<<atof(line.c_str());
else cout<<"loi";



Welcome, I have to write your reference. I'm learning how to check the number entered is correct or not ( ie not have letters or characters invalid.
– When I used to enter it back char not know how to convert from char to double.
– Refer your way, it has a drawback that if import: 123BC4, the result is 123. Is there any way this case is not an error?
On its side with the fail inspection() dirty.
have you solved this error yet !!