Thread: Help understanding code

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Help understanding code

    I found this code on the internet and I don't understand whats the point of this line : (str[i] - '0')

    I know its not working without it but whats the point of subtracting 0 of something?

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    int string2sec(const std::string& str)
    {
        int i = 0;
        int res = -1;
        int tmp = 0;
        int state = 0;
    
        while(str[i] != '\0')
        {
            // If we got a digit
            if(str[i] >= '0' && str[i] <= '9')
            {
                tmp = tmp * 10 + (str[i] - '0');
            }
            // Or if we got a colon
            else if(str[i] == ':')
            {
                // If we were reading the hours
                if(state == 0)
                {
                    res = 3600 * tmp;
                }
                // Or if we were reading the minutes
                else if(state == 1)
                {
                    if(tmp > 60)
                    {
                        return -1;
                    }
                    res += 60 * tmp;
                }
                // Or we got an extra colon
                else
                {
                    return -1;
                }
    
                state++;
                tmp = 0;
            }
            // Or we got something wrong
            else
            {
                return -1;
            }
            i++;
        }
    
        // If we were reading the seconds when we reached the end
        if(state == 2 && tmp < 60)
        {
            return res + tmp;
        }
        // Or if we were not, something is wrong in the given string
        else
        {
            return -1;
        }
    }
    
    int main()
    {
    
        std::cout << string2sec("01:01:05") << std::endl;
    //    std::cout << string2sec("1:01:01") << std::endl;
    //    std::cout << string2sec("1:2:0") << std::endl;
    //    std::cout << string2sec("10:10:10") << std::endl;
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    That's not a zero, that's the character code of '0', which in ASCII is 48, and all of the digits are in order ('1' is 49, '2' is 50, ..., '9' is 57). To append a digit to tmp, you multiply it by 10 and then add that digit to it. If you don't subtract '0', you would add an extra 48 instead which you don't want.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you.

    So then why when I do this it wont change the value of str?

    Code:
        
        string str = "103";
    
        cout << "result: " << str[0] - '0' << "\n\n";
        cout << "result: " << str[1] - '0' << "\n\n";
        cout << "result: " << str[2] - '0' << "\n\n";
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    Why would it? You're not assigning anything, you're just doing the subtractions.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    958
    When you do str[0] - '0', I believe the result is automatically promoted to an int, and when you send that int value to cout it's converted to its text representation, so you'll see "1", "0", and "3" printed.

    Try assigning 0 to an int variable and print it out, and then assign '0' to the variable and print it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Understanding This Code.
    By thisbeme in forum C Programming
    Replies: 11
    Last Post: 03-04-2011, 12:01 PM
  2. Need help to understanding the code
    By zbonzbon in forum C Programming
    Replies: 10
    Last Post: 02-13-2011, 10:23 PM
  3. help me understanding this code
    By apfelsaft in forum C Programming
    Replies: 2
    Last Post: 01-14-2004, 04:46 PM
  4. help understanding code
    By volk in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 10:50 AM
  5. Code Understanding!
    By Jill n Jall in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 04:16 PM

Tags for this Thread