Home > Back-end >  Determine whether or not as an integer
Determine whether or not as an integer

Time:10-01

Have bosses know how to write a program to judge whether the input data as an integer? I know with cin. Fail and cin. Ignore can realize judge input the first number, but if the input is 3 a this, it will judge twice, print out the two results, what can discriminate the one-time?

CodePudding user response:

No 3 a method named this

CodePudding user response:

reference 1st floor qq_46221910 response:
no 3 a method named this

Is not named, as input data, it can only judge the first number, the combination type of judgment not to come out

CodePudding user response:

If you have to use the library function,,
#include

Long int strtol (const char * NPTR, char * * endptr, int base);
Is what you want,
NPTR will point to the first figures,
You use first cin deposit the string to the string, and then call this function to judge, example below
 # include & lt; Stdlib. H> 
#include

using namespace std;

Int main ()
{
String STR.
Long val.
Char * endptr;

cin> Val.
Const char * str_ptr=STR. C_str ();

Val=strtol (str_ptr, & amp; Endptr, 10);

If (endptr!=NULL) {
Cout & lt; <"Not int" & lt; }
The else {
Cout & lt; <"Yes int" & lt; }
return 0;

}


CodePudding user response:

First unified using a string to read data, and then judge whether a string is digital, judge integer is nothing more than to judge each character is digital, can use the isdigit function, it also need to pay attention to the first may be a minus sign, another way is to c + + istringstream, this string of characters used to enter an int variable, successful and determine if input buffer is empty

CodePudding user response:

 string integer; 
While (cin & gt;> Integer) {
Bool flag=true;
For (auto c: integer) {
if (! Isdigit (c))
Flag=false;
}
If (flag)
Cout & lt; <"Yes integer!" The else
Cout & lt; <"No integer!"
}

To a different

CodePudding user response:

reference 5 floor s_father reply:
 string integer; 
While (cin & gt;> Integer) {
Bool flag=true;
For (auto c: integer) {
if (! Isdigit (c))
Flag=false;
}
If (flag)
Cout & lt; <"Yes integer!" The else
Cout & lt; <"No integer!"
}

To a different

Can you explain for statement cycle conditions what it is?

CodePudding user response:

The
reference 3 floor yukint response:
if you have to use the library function,,
#include

Long int strtol (const char * NPTR, char * * endptr, int base);
Is what you want,
NPTR will point to the first figures,
You use first cin deposit the string to the string, and then call this function to judge, example below
 # include & lt; Stdlib. H> 
#include

using namespace std;

Int main ()
{
String STR.
Long val.
Char * endptr;

cin> Val.
Const char * str_ptr=STR. C_str ();

Val=strtol (str_ptr, & amp; Endptr, 10);

If (endptr!=NULL) {
Cout & lt; <"Not int" & lt; }
The else {
Cout & lt; <"Yes int" & lt; }
return 0;

}

Compile failed

CodePudding user response:

What's wrong with what the compiler, report?

CodePudding user response:

refer to the eighth floor yukint response:
what compiler, what's wrong?

Dev - c [error] Id returned 1 exit status

CodePudding user response:

How to baidu, you set its support for c99, I will not send a screenshot

CodePudding user response:

reference response: 6th floor city China
Quote: refer to the fifth floor s_father reply:
 string integer; 
While (cin & gt;> Integer) {
Bool flag=true;
For (auto c: integer) {
if (! Isdigit (c))
Flag=false;
}
If (flag)
Cout & lt; <"Yes integer!" The else
Cout & lt; <"No integer!"
}

To a different

Can you explain for statement cycle conditions what it is?

The new usage of c + + 11 for loop, the function of the specific you can baidu came out,
  • Related