how to check if input is integer ?

Hi. I have an assignment to do..i have done it..but i need to do one more thing. Things sound like this:
user inputs 6 integers
program needs to check them if there are integer if not it has to output a message for the user
if the input is integer it has to go further and work with the input

any guideliness?
i have used this structure :
if ( ! ( cin >> temp ) )

{
cout<<"Input is not integer.This program will end ! "<<endl<<endl<<endl;

system("pause");
return 0;

}

where i declared temp as being int since i started

the problem is after it gets the last input still waits for an input
i will attach the source code if needed.
thank you all in advance
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include< iostream>

using namespace std;

int main(){

     int input = 0;;

     cout << "Enter an integer : ";
     cin  >> input;

      while( cin.fail() ){
              cin.clear();
              cin.ignore(100,'\n');
              cout << "Input is not integer ! Please re-enter ! " >> endl;
              cout << "Enter an integer : ";
              cin  >> input;
      }

      //After input correct . something like
      if( input == 0 ){
             cout << "Input is 0 " << endl;
      }
      else{
          //...Just example
      }
      
      system( "pause" );
      return 0;
}


easy to understand , think it =)
Last edited on
1
2
3
4
double input;
cin >> input;
while( cin.fail()  || static_cast<int>(intput) != input)
  //... 
thank you for your help but I need to implement it in my source code

#include<iostream>
using namespace std;
int main()
{
double a_one,a_two,b_one,b_two,c_one,c_two; // we allocate space in the memory for the values that user has to input
double x,y,d,d_x,d_y;


cout<<" input a_one"<<endl;
cin >>a_one;

cout<<" input b_one"<<endl;
cin>>b_one;

cout<<" input c_one"<<endl;
cin>>c_one;

cout<<" input a_two"<<endl;
cin>>a_two;

cout<<" input b_two"<<endl;
cin>>b_two;

cout<<" input c_two"<<endl;
cin>>c_two;



cout<<"Now to calculate the solutions for you !"<<endl<<endl;


if ((a_one*b_two)!=(a_two*b_one))

{
d=a_one*b_two-a_two*b_one; //we calculate the determinant

d_x=(c_one*b_two)-(c_two*b_one);
d_y=(a_one*c_two)-(a_two*c_one);

x=d_x/d;
y=d_y/d;

cout<<" The solutions are : "<<endl<<endl;
cout<<"x="<<x<<endl<<endl;
cout<<"y="<<y<<endl<<endl;

}

else
cout<<"d=0 . I can't go further"<<endl;

system("pause");
return 0;

}




I need to verify the input . if the input is double or integer then the program should work. if not it needs to ask the user to input the corect value and then i need the program to use it.

thanks in advance
Topic archived. No new replies allowed.