double a,b;
cout<<"Enter Value";
cin>>a>>b;

jus a simple program...whenever a character is Key in ..the program error.i know is because of the double only for integer.how can i edit it so that it will return me a cout<<"U have to enter a integer"; when character is enter. anyone can help ? is it i have to change them to string?

yes, reading all your input as a string is one reliable way to do it (That would be my preference). The other reliable way is to use cin as an 'if' or 'while' condition. (Not so great, because you have to deal directly with the cin error flags every time)

#include <iostream>
#include <limits>
#include <sstream>
#include <string>

bool read_double(double& d)
{
    std::string str;
    std::cin >> str;  //Alternatively, use std::getline()
    std::stringstream ss(str);
    if( ss >> d )
        return true;
    else
    {
        //Possibly still bad characters left in cin
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
        return false;
    }
}

int main()
{
    double a, b;
    while( !read_double(a) || !read_double(b) )
        std::cout << "Error" << std::endl;
}

or

#include <iostream>
#include <limits>

int main()
{
    double a,b;
    while( ! (std::cin >> a >> b) )
    {
        std::cout << "Invalid Input!" << std::endl;
        std::cin.clear();   //Clear error flags
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }
}

Getting input from the user is always a little bit messy, there's no clean, easy way about it unfortunately.

#include <iostream>
#include <limits>
using namespace std ;

inline double get_double()
{
  double value ;
  if( cin >> value ) return value ;
  
  cout << "you have to enter a number!\n" ;
  cin.clear() ;
  cin.ignore( numeric_limits<streamsize>::max(), '\n' ) ;
  return get_double() ;
}
#include <iostream>
#include <math.h>
using namespace std;

class Point
{
	private:
			double x1, x2, x3, y1, y2, y3;
               public:
		void getPointA();
};
void Point::getPointA()				
{
	double a, b;
	cout << "\n\nPlease enter co-ordinates for point A : ";
	cin >> a >> b;
????????????????????????????????????????
	x1 = a;
	y1 = b;
}

Some working to be carried out below .. how can i modify this? can i apply those example in the ? show above

i'm suppose to key in 2 value , but need to display error whenever character is entered. thus end the program. can i still able to string all of them?

I think you missed the underlying message - which is that you need to change this line, either to check cin, or to get cin to read 'a' and 'b' as strings.

cin >> a >> b;

Whichever way you do it, you will either need to have the cin code repeated to ask the user again, or to drop out of the block/function completely, passing back an error to the callier

i'm suppose to key in 2 value , but need to display error whenever character is entered. thus end the program. can i still able to string all of them?

You've been given 3 examples. Maybe you should try to use one of them.

Be a part of the DaniWeb community

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