0

Possible Duplicate:
std::string to float or double

I am writing a calculator (learning C++), and just decided to make a calculator, since that was the first thing I did when learning Java.

The program does the following:

  1. Asks the user for the first number
  2. Asks what the user wants to do with the number (-,+,*,/)
  3. Asks for the second number
  4. Displays the result.

when grabbing a number from the user in Java I used Double.parseDouble(number) to check if what they entered is a number or not.

Is there a similar command in C++? Ive been doing research and it seems like you have to use tricks such as comparing it to ASCII equivalents etc.. basically a ton of code for a simple task... so before i take that route, I wanted to stop by here and see if perhaps there is some sort of call I can make to check if the input is a number. I need it to validate negatives, zero and positives, as well as numbers with decimals... everything else should be rejected and the user should be asked for input again.

When I did it in Java I used try/catch statement and if the input was invalid it would return the method (in other words, itself) so it would loop and ask the user for input again.

Thanks!

Community
  • 1
  • 1
Duxa
  • 966
  • 2
  • 14
  • 27

3 Answers3

4

You can use strtod. It handles underflow and out of range values in a convenient way.

Additionally, as Joachim Pileborg notes, if you use C++11 compliant compiler, there is std::stod in the standard library.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • 2
    Also, in C++11 there is [`std::stod`](http://en.cppreference.com/w/cpp/string/basic_string/stof). – Some programmer dude Oct 05 '12 at 09:25
  • didn't know about that. I have added it to the answer. – Zdeslav Vojkovic Oct 05 '12 at 09:27
  • Im using Visual Studio 2010, dont think it supports stod :/ – Duxa Oct 05 '12 at 09:43
  • VS 2010 supports `std::stod`, just `#include `. however, you can always use `strtod`. – Zdeslav Vojkovic Oct 05 '12 at 09:47
  • Ok thanks, forgive my newbiness. The class Im taking just told us to create Hello World, that was easy.. i dont want to wait till next week for the next assignment, so Im doing in this in meantime.... I am trying to use stod but am having problems understanding the syntax. like what is the *pos thing? Im not sure what it means by where to store the first integer.. here is a function Im trying: double getNumber() { string getNum = 0; double num = 0; cin >> getNum; stod(const string& getNum, num = 0 ); return num; } – Duxa Oct 05 '12 at 09:51
  • `pos` is a pointer to a variable which will take the index of first unconverted character, if the string is not a valid number. E.g. when converting `"12w34"`, `pos` would contain value `2`, because that is the index of character `w`. If you set it to `NULL`/`nullptr` or omit it completely (as it's default value is 0), then it will be ignored. – Zdeslav Vojkovic Oct 05 '12 at 10:14
  • BTW, the `num = 0` notation is only for function declaration, where it specifies the default value of the argument (i.e. the value to be used if argument is omitted). when you call the function you either specify it normally, or ommit it completely: `stod(s, &n);` or `stod(s, NULL);`. The last one is the same as `stod(s);` – Zdeslav Vojkovic Oct 05 '12 at 10:16
  • awesome, thank you.. i got both stod and strtod to work :) i prefer stod though since it is a bit cleaner and since its newer it probably works better and is easier to work with... thanks again for your help. – Duxa Oct 05 '12 at 10:23
  • Be really careful with locale-s. Strtod will use the system locale. Which can cause surprises. – tothphu Aug 18 '14 at 03:05
0

Use the function double atof(const char*) ;

example usage:

const char* = "3.14159";
double pi = atof(myDouble);
remi
  • 3,914
  • 1
  • 19
  • 37
-1

How about using isdigit function.

ayush
  • 14,350
  • 11
  • 53
  • 100