-
string to int?
I am taking an Intro to C++ class and I am having trouble with my current project. I have to create a bank account with information from a .txt file.
One of the bits of information from the file is an account balance, which is: $2500.20. To get it to pop up on the screen, I had to declare it as a string because of '$' sign.
My question is:
Is there a way to change $2500.20 to an int or float so that I can perform basic math functions such as addition/subtraction (which are the deposit/withdraw functions for my project) on it?
I looked in all my books and can't make sense of anything. I also searched online and found someone mention:
dblVariable = cdbl(string)
I tried that and got an error message saying that "'cdbl' cannot be used in a function." I don't even know what cdbl means, and I don't know how to fix this! Am I making any sense? Can someone PLEASE help?
-
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
That's just for ints though. I would recommend tokenizing the string so that you have the number before the decimal point, and the number after. Once you've converted both to ints, just multiple the second number by .01 (you'll have to type cast to a float) and add it to your first number.
-
std::stringstream;
Code:
std::stringstream stream("$2500.20");
char temp_char;
double cash;
//Remove the $ character
stream >> temp_char;
stream >> cash;