网上是用 cin.fail()
但是遇到输入 1a等数字开头的输入并不会认为是非法输入,所以无法判断如1a、1.2(浮点型)的输入
// Q761940.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <stdio.h>
#include <stdlib.h>
int isint(char* s)
{
while (*s != '\0')
{
if (*s < '0' || *s > '9')
return 0;
s++;
}
return 1;
}
int main()
{
char buf[100];
scanf_s("%s", &buf[0], 100);
if (isint(buf))
{
int x = atoi(buf);
printf("%d\n", x);
}
else
{
printf("invalid int\n");
}
}
C++:
#include <typeinfo>
int L;
std::cout << typeid(L).name() << std::endl;
C:
#include <ctype.h>
int isalnum(int c);
当此时数字或字母是返回非0, 否则返回0
或者使用:
int isdigit(int c); figure (0-9)