I'm confused with a++ line.



  • Hello, everyone!

    I have a program that removes the amount of the cypher in it.

    Let's get a line. "qwrtt56hhbb055ghjj"♪ Accordingly, the programme withdraws "5 цифр"

    How do I find their money now? You mean 5+6+0+5+5?



  • I think it's easier to use normal cycles here.

    This is a demonstration programme that shows two approaches, depending on where the line is stored.

    #include <iostream>
    #include <string>
    

    int main()
    {
    {
    const char *s = "qwrtt56hhbb055ghjj";
    unsigned int sum = 0;

    for ( const char *t = s; *t; ++t )
    {
        if ( *t &gt;= '0' &amp;&amp; *t &lt;= '9' ) sum += *t - '0';    
    } 
    
    std::cout &lt;&lt;  "sum = " &lt;&lt; sum &lt;&lt; std::endl;
    

    }
    {
    std::string s( "qwrtt56hhbb055ghjj" );

    unsigned sum = 0;
    
    for ( char c : s )
    {
        if ( c &gt;= '0' &amp;&amp; c &lt;= '9' ) sum += c - '0';    
    } 
    
    std::cout &lt;&lt;  "sum = " &lt;&lt; sum &lt;&lt; std::endl;
    

    }

    return 0;
    

    }

    In both cases, the consolation will be

    sum = 21
    sum = 21


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2