1

Possible Duplicate:
How to determine if a string is a number with C++?

I have written a very simple calculator program in C++. Here it is:

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

int main()
{
   double num1;
   double num2;
   string op;
   double num3;
   int x;
   bool y = false;

   do
   {
      cout<<"Press t to terminate the application"<<endl;

      cout<<"Enter the first number"<<endl;
      cin>>num1;

      cout<<"Enter the operator"<<endl;
      cin>>op;
      cout<<"Enter the next number"<<endl;
      cin>>num2;

      if(op=="/"&&num2==0)
      {
         cout<<"You are attempting to divide by 0. This is impossible and causes the destruction of the universe. However, the answer is infinity"<<endl;
         y = true;
      }

      if(y==false)
      {
         if(op=="+") {
            num3 = num1+num2;
         }
         else if(op=="-") {
            num3 = num1-num2;
         }
         else if(op=="*"||op=="x"||op=="X") {
            num3 = num1*num2;
         }
         else {
            num3 = num1/num2;
         }
         cout<<endl;
         cout<<endl;
         cout<<"Answer:"<<num3<<endl<<endl;
      }
   } while(x!=12);

   return 0;
}

As you can see, I want to allow people to terminate the application by pressing 't'. This obviously won't work because cin will try and assign a letter to a double (if I do press 't' the application crashes). I am planning to use strings instead to get the input, but how would I test if the string is a letter or a number?

Community
  • 1
  • 1
imulsion
  • 8,206
  • 18
  • 46
  • 81
  • I'd check if it's "t" first and then use `boost::lexical cast` to check if it is a number. – chris Nov 04 '12 at 20:30

4 Answers4

4
#include <cctype>

and use isalhpa(), isdigit(), isalnum() on string contents?

Michael Krelin - hacker
  • 122,635
  • 21
  • 184
  • 169
1

Here is sample and working code, just change it so it suits your needs

#include <iostream>
#include <string>
#include <cctype>
#include <stdlib.h>

using namespace std;

bool isNum(char *s) {
    int i = 0,  flag;

    while(s[i]){
            //if there is a letter in a string then string is not a number
        if(isalpha(s[i])){
            flag = 0;
            break;
        }
        else flag = 1;
        i++;
        }
    if (flag == 1) return true;
    else return false;
}


int main(){
    char stingnum1[80], stringnum2[80];
    double doublenum1, doublenum2;
    cin>>stingnum1>>stringnum2;
    if(isNum(stingnum1) && isNum(stringnum2)){
        doublenum1 = atof(stingnum1);
        doublenum2 = atof(stringnum2);
        cout<<doublenum1 + doublenum2 << endl;
    } 
    else cout<<"error";

   return 0;
}
hyperN
  • 2,524
  • 6
  • 49
  • 87
0

You can input into a string, and then use the following function:

int atoi ( const char * str );

If the string is numerical it will translate it into an integer.

If the string isn't numerical it will return 0: in which case you can check only the first character of the string, if its a zero then consider the input as a 0. If the first character isn't a zero consider the string as not numeric.

Roy Spector
  • 151
  • 10
0

Well if you only check for 't' you can do it stupid and easy way.

     if(stringnum1== 't' || stringnum2== 't') {

            //terminate
       }
 else {
           doublenum1 = atof(stringnum1)
           doublenum2 = atof(stringnum1)
           // your math operations
           }

Better way would be:

if(isalhpa(stringnum1) || isalpha(stringnum2)){
                 //terminate
           }
      else {
           doublenum1 = atof(stringnum1)
           doublenum2 = atof(stringnum2)
           // your math operations
           }

P.S.

if you want to test string and not char, here is sample: link the best way would be to make function to test if given string is number or not if it is number return true, else return false (or other way around)

hyperN
  • 2,524
  • 6
  • 49
  • 87