How can I tell if an input is a numeric value?

Well I'm making a simple calculator, anyways if you type non-numeric values it goes all crazy, how can I use std::cin.ignore(); or another function to tell me if it's a non-numeric value?

Thanks for help!!!

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// BasicCalculator.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath>

int _tmain(int argc, _TCHAR* argv[])
{
   system("COLOR 02");
   system("TITLE Basic Calculator by CometJack");
   float fltNum1, fltNum2;
   char chrOper, chrAns;
   do
   {
       std::cin.clear();
       system("CLS");
       std::cout << "Please type in your 1st numeric value." << std::endl;
       std::cin >> fltNum1;
       system("CLS");
       std::cout << "Please select on the following operators: +, -, *, /, ^ or $." << std::endl;
       std::cin >> chrOper;
       system("CLS");
       if (chrOper != '$')
       {
       std::cout << "Please type in your 2nd numeric value." << std::endl;
       std::cin >> fltNum2;
       system("CLS");
       }
           switch (chrOper)
         {
       case '+':
          std::cout << fltNum1 << '+' << fltNum2 << '=' << fltNum1 + fltNum2 << std::endl;
          break;
       case '-':
          std::cout << fltNum1 << '-' << fltNum2 << '=' << fltNum1 - fltNum2 << std::endl;
          break;
       case '*':
          std::cout << fltNum1 << '*' << fltNum2 << '=' << fltNum1 * fltNum2 << std::endl;
          break;
       case '/':
           if (fltNum2 != 0)
           {
               std::cout << fltNum1 << '/' << fltNum2 << '=' << fltNum1 / fltNum2 << std::endl;
           }
           else
           {
               std::cout << fltNum1 << '/' << fltNum2 << '=' << "undefined" << std::endl;
           }
            break;
       case '^':
           std::cout << fltNum1 << '^' << fltNum2 << '=' << pow(fltNum1, fltNum2) << std::endl;
           break;
       case '$':
           std::cout << '$' << fltNum1 << '=' << sqrt(fltNum1) << std::endl;
           break;
       default:
           std::cout << "Sorry, you didn't follow directions, now restart." << std::endl;
           break;
          } 
       std::cout << "Enter Y if you'd like to restart, otherwise press any key to cancel." << std::endl;
       std::cin >> chrAns;
    } while (chrAns == 'Y' || chrAns == 'y');
   return 0;
}
Example from http://cplusplus.com/reference/iostream/istream/peek/

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
// istream peek
#include <iostream>
using namespace std;

int main () {
  char c;
  int n;
  char str[256];

  cout << "Enter a number or a word: ";
  c=cin.peek();

  if ( (c >= '0') && (c <= '9') )
  {
    cin >> n;
    cout << "You have entered number " << n << endl;
  }
  else
  {
    cin >> str;
    cout << " You have entered word " << str << endl;
  }

  return 0;
}


But if you are looking for error handling something like this will do:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
    int n;
    if (!(cin >> n)) {
         cin.clear();
         while(cin.get() != '\n');
    }
    do_more();
    return 0;
}


Do you understand the code?

Maikel
Last edited on
Some great methods by Zaita: http://www.cplusplus.com/forum/articles/6046/
Are you looking to completely prevent the user from typing in non numeric values or do you just need to identify when non numeric values are present and prompt for invalid entry?
Topic archived. No new replies allowed.