D
Drew
Hi All:
I know I am missing something easy but I can't find the problem! I
have a program which reads an integer as input.
The output of the program should be the sum of all the digits in the
integer that was entered.
So, if 353 was entered, the output should be 11.
I'm reading an integer, converting it to a string called intstring
with sprintf.
I'm then getting the length of the string and going thru a while loop
using that. Each time thru the while loop, I stuff the current
character of the string into a char variable ch.
Then I attempt to convert the character into an integer using atoi and
the address of ch. I was thinking that should give me the int value
of the char.
Then, I'm just adding the current integer value to a running
accumulator. That should do it!
The problem appears to be in the y = atoi(&ch); statement.
Somehow, y is assigned in the first iteration 3354, when it should
just be 3. The correct result of 3 appears to get combined with the
integer I entered, 354. I can't figure out how this is happening!
Any help would be appreciated. I have to be missing something easy!
Thanks!
Drew
My debugging output looks like this:
Enter an integer: 354
string is 354
ch is 3
y is 3354
ch is 5
y is 5354
ch is 4
y is 4354
The sum of the digits of 354 is 13062
Here's my code:
#include <stdio.h>
int main()
{
int x, y, z;
char intstring[25];
char ch;
int intstring_length;
int running_sum;
printf("Enter an integer: ");
scanf("%d", &z);
sprintf(intstring, "%d", z);
intstring_length = strlen(intstring);
printf ("string is %s\n", intstring);
x = 0;
running_sum = 0;
while (x < intstring_length)
{
ch = intstring[x];
printf ("ch is %c\n",ch);
y = atoi(&ch);
printf ("y is %d\n\n", y);
running_sum = running_sum + y;
x = x + 1;
}
printf("The sum of the digits of %s is %d", intstring, running_sum);
} // END OF MAIN
I know I am missing something easy but I can't find the problem! I
have a program which reads an integer as input.
The output of the program should be the sum of all the digits in the
integer that was entered.
So, if 353 was entered, the output should be 11.
I'm reading an integer, converting it to a string called intstring
with sprintf.
I'm then getting the length of the string and going thru a while loop
using that. Each time thru the while loop, I stuff the current
character of the string into a char variable ch.
Then I attempt to convert the character into an integer using atoi and
the address of ch. I was thinking that should give me the int value
of the char.
Then, I'm just adding the current integer value to a running
accumulator. That should do it!
The problem appears to be in the y = atoi(&ch); statement.
Somehow, y is assigned in the first iteration 3354, when it should
just be 3. The correct result of 3 appears to get combined with the
integer I entered, 354. I can't figure out how this is happening!
Any help would be appreciated. I have to be missing something easy!
Thanks!
Drew
My debugging output looks like this:
Enter an integer: 354
string is 354
ch is 3
y is 3354
ch is 5
y is 5354
ch is 4
y is 4354
The sum of the digits of 354 is 13062
Here's my code:
#include <stdio.h>
int main()
{
int x, y, z;
char intstring[25];
char ch;
int intstring_length;
int running_sum;
printf("Enter an integer: ");
scanf("%d", &z);
sprintf(intstring, "%d", z);
intstring_length = strlen(intstring);
printf ("string is %s\n", intstring);
x = 0;
running_sum = 0;
while (x < intstring_length)
{
ch = intstring[x];
printf ("ch is %c\n",ch);
y = atoi(&ch);
printf ("y is %d\n\n", y);
running_sum = running_sum + y;
x = x + 1;
}
printf("The sum of the digits of %s is %d", intstring, running_sum);
} // END OF MAIN