Search code examples
c++inputuser-input

Read in Dollar Amount (with dollar sign) C++


I need to read in a value (cin) including a dollar sign and a dollar amount, but I only want to read in the amount.

In other words, I have this input: "$3458.5," but I want to read in and store "3458.5"

The current approach I thought of was reading the value in as a string, removing the first character, and then converting to a double. But, I feel like this method is inefficient and there's a better method out there. Any tips? Thanks!


Solution

  • I agree with Magnus: this seems minor. But if you really want to do it, just read a character then read a double:

    char ch;
    double d;
    std::cin >> ch >> d;