Thread: string to int?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    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?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    std::stringstream;
    Code:
    std::stringstream stream("$2500.20");
    
    char temp_char;
    double cash;
    
    //Remove the $ character
    stream >> temp_char;
    stream >> cash;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM