Now, i input 1.5, obviously, it will show 1. But how can the program check that i just input 1.5 so that it will show an error for non integer input?

#include<iostream>
using namespace std;
int main(){
    int k;
    cout<<"Input: ";
    cin>>k;
    cout<< k ;
}

Recommended Answers

The basic way is to save the input into a string and then check each character for validity.

Jump to Post

All 3 Replies

Why don't you use a float type to get the value and then check for non integer

The basic way is to save the input into a string and then check each character for validity.

you should use a float or double type instead int.
int just used for numbers without float point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.