Sum of a string via /r/learnprogramming


Sum of a string

I am trying to calculate the sum of a string. The following code works fine if there is no condition(no if statement). However, as soon as I put the condition, it gives me the sum 0. How can I fix this?

string test01 = "12364"; int sum = 0; for (int i = 0; i < test01.size(); i++){ if (test01.at(i) > 0 && test01.at(i) < 6){ // without this, it works fine sum += test01.at(i) - '0'; } } 

Submitted July 13, 2017 at 03:40AM by iitxmpii
via reddit http://ift.tt/2uU4nBz

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s