tags:

views:

4944

answers:

8

Hello all, i'm new here. i found this site on google.

#include <iostream>

using namespace std;

void main() {

    // Declaration of Variable
    float num1=0.0,num2=0.0;

    // Getting information from users for number 1
    cout << "Please enter x-axis coordinate location : ";
    cin >> num1;

    // Getting information from users for number 2
    cout << "Please enter y-axis coordinate location : ";
    cin >> num2;

    cout << "You enter number 1 : " << num1 << " and number 2 : " << num2 <<endl;

I need something like, when users enter alphabetical characters, it would display an error says, you should enter numbers.

Any help greatly appreciated

+1  A: 

Use something like

if (static_cast<int>(num1) == num1) {
  // int value
}
else {
  // non-integer value
}
Blair Zajac
You should use static_cast over a C-style cast.
GMan
+1  A: 

The input will be cast to fit the variable you're storing with cin. Because you're using cin on num1 and num2 (which are floats), no matter what number the user enters (to a degree), it will be a float.

Ryan Van Antwerp
+2  A: 

If you want to check input for integer/float/neither you should not use cin into a float. Instead read it into a string and you can check whether or not the input is valid.

If cin reads an invalid number it will go into a failed state, which can be checked with if(cin.fail())

However it is easier to read the input as a string and then convert the input to an integer or floating point number afterwards.

isdigit(c) should be called on a character not an an integer. For example isdigit('1') (Note the quotes).

you can use strtol to attempt to convert a string to an integer. Depending on the result you can then attempt to convert to floating point.

Steven
`strotol` isn't standards-compliant. Furthermore, I fail to see why reading into a string and then using `isdigit` or manual parsing should be easier than checking the fail state of the stream.
Konrad Rudolph
+2  A: 

Although others have already answered the question, I'd just like to point out what isdigit is really used for. It tells you whether a given character represents a number or not.

Basically, the definition of isdigit may be:

int isdigit (int c)
{
    if (c >= '0' && c <='9')
        return 1;
    else
        return 0;
}

Then, if you have a string "asdf1234", you can check each character individually to test if it is a digit/number:

char *test = "asdf1234";
int i;

for (i = 0; i < strlen (test); i++)
{
    if (isdigit (test[i]))
        fprintf (stdout, "test[%d] is a digit!\n", i);
}
dreamlax
Yes. You must also accept minus, plus, thousand and decimal separators used in the current locale (not only 0-9)
Jem
Should probably also accept leading and trailing spaces. Those don't pass "isdigit", but can still be valid parts of input (though, should be stripped away as early as possible).
abelenky
@Jem: The ANSI C definition of isdigit does not check for plus, minus, grouping or decimal separators. isdigit returns non-zero only if the parameter is a digit, and nothing else. Test it on your system and see. A proper number parser would use a function more comprehensive than isdigit, but the purpose of my answer was only to explain isdigit.
dreamlax
That was the purpose of my comment: one can use isdigit to check if a character can be in a number, as soon as he "ALSO" check himself for minus, plus...
Jem
A: 

Always read the number in a string and check the same as below:-

template <class out_type, class in_type>
out_type convert(in_type const& t)
{
  std::stringstream stream;
  stream << t; // insert value to stream
  // store conversion’s result here
  out_type result;
  stream >> result; // write value to result
  // if there is a failure in conversion the stream will not be empty
  // this is checked by testing the eof bit
  if (! stream.eof() ) // there can be overflow also
  { // a max value in case of conversion error
    result = std::numeric_limits<out_type>::max();
  }
  return result;
}

It is used as

int iValue = convert<int>(strVal);
if (std::numeric_limits<int>::max() == iValue)
{
  dValue = convert<double>(strVal);
}

This is a little modern way of doing it :-)

Abhay
A: 
Andreas Bonini
+5  A: 

I'd use the cin.fail() approach or Boost "lexical cast" with propper use of exceptions to catch errors http://www.boost.org/doc/libs/1_38_0/libs/conversion/lexical_cast.htm

piotr
+4  A: 

First, to answer your question. This is actually very easy and you don't need to change much in your code:

cout << "Please enter x-axis coordinate location : " << flush;
if (!(cin >> num1)) {
    cout << "You did not enter a correct number!" << endl;
    // Leave the program, or do something appropriate:
    return 1;
}

This code basically checks whether the input was validly parsed as a floating point number – if that didn't happen, it signals an error.

Secondly, the return type of main must always be int, never void.

Konrad Rudolph