Searched your forums a lot, found a solution to my task, but still I do not understand why do I get the wrong answer everytime. It's always a 'correct-1' result. It doesn't depend on what the number in the string is.

#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
    string s1;
    int i, x, y;
    char temp;
    s1="1020401";
    for(i=0;i<s1.length();i++) {
        temp=s1[i];
        x=x+atoi(&temp);                            
    }
    cout<<endl<<x<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

this gives me 7, but the correct result must be 8, what did I miss?

p.s. maybe something is written not good and some of you may show me my mistakes.

Comments
Good first post.

There's a few small errors in your code, but overall you seem to have the right idea. Here is the corrected code:

#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
  string s1;
  int x = 0;

  s1 = "1020401";
  int length = (int)s1.length();

  for (int i = 0; i < length; i++) {
    if ( isdigit(s1[i]) ) {
      x += s1[i] - '0';
    }
  }

  cout << x << endl;

  cin.ignore();
  return EXIT_SUCCESS;
}

As this is C++ and not C, I didn't see much need in declaring i outside the for loop. Also y is never used, so I removed that too.

Your main problem was this part:

temp=s1[i];
x=x+atoi(&temp);

atoi is used for converting a c-string such as "123" to 123, and this function requires the string ends with a null-terminator. All you need to do is check if each character is a digit (using isdigit) and then add the value to x.

Also, you should avoid system() calls, and just stick to cin.ignore() to pause your program before it closes.

Hope this helps.

thank you very much for all the corrections and the explanations.
to make the answer right all I had to do is to write x=0; in the declaration.

though, I have more questions about your code.

x += s1[i] - '0';

what is the - '0' ? I mean I want to know how this works and how does it replace atio()?

and why is it better to write

int length = (int)s1.length();

than
simply using s1.length() in 'for' ?

>what is the - '0' ? I mean I want to know how this works and how does it replace atio()?
Each character has a value in the ascii table, for example the letter 'a' has a numerical value of 97. The character '5' has a value of 53.

So that means to find the true value of a character that is a digit, you have to subtract '0' away from it. Here's an example:

'7' has a value of [B]55[/B]
'0' has a value of [B]48[/B]

55 - 48 = [B]7[/B]

>how does it replace atio()?
atoi works differently than you think, and shouldn't be used in this case. atoi actually converts a string to a number. Here's an example:

char mystr[] = "123";
int mynum = atoi(mystr); // mynum contains a value of 123

>and why is it better to write int length = (int)s1.length(); than simply using s1.length() in 'for'?
It's good practice to make sure your compiler throws no warnings at you, and in this case my compiler did (as s1.length() returns the data type size_t and not an int). To remove the error, you have to typecast it with (int). Also, I took it out of the for loop as once again, it is good practice. Every time you check to see if i has reached the length of the string, you are calling the .length() function. In more extreme cases, this could slow things down a lot, here's another example:

#include <iostream>
#include <cmath>
using namespace std;

int main(int argc, char *argv[]) {
  double number = 1234567890;

  for (double i = 0; i < sqrt(number); i += 1) {
    // Do stuff
  }
}

Here, the sqrt function is called 35136 times, and as calculating the square root of a number is a slow process, you would ideally only want to calculate it once.

Hope these answer your questions.

I have also attached each character value.

Comments
I like your approach :)

Big thanks, now I understand it clearly. I know what is ASCII and how the characters are represented, I just didn't know it returns the numerical value and not the character(in your example - the 7th).
Once again, thank you, it's great to see fast and informative responses that really help!

This article has been dead for over six months. Start a new discussion instead.