# Thread: Sum of octals found in input

1. ## Sum of octals found in input

I really want to understand character I/O in C but I feel like my understanding is faulty about certain concepts. I wanna write a program that gives me the sum of all base-8 numbers found in input, but the sum is never incremented.
Code:
```/* Write a function that reads data from standard input until EOF is found and returns the sum of the base-8 numbers found into the input.
We consider a number in base-8 a sequence that starts with 0 and is followed by digits from range [0-7].
The numbers must be separated from other words by at least one whitespace.
All the numbers and the final sum are guaranteed to fit in "unsigned" data type. */

#include <stdio.h>

int main(){
int current, last;
unsigned sum=0;
int n = 0;
printf("Enter text:\n");
current = getchar();
while(current != '\n'){
putchar(current);
last = current;
current = getchar();
if(last == '0' && isdigit(current)){
last = current;
current = getchar();
while(isdigit(current)){
n = current- '0';
sum = sum+n;
}
}
}

printf("\nThe sum is %d", sum);
return 0;
}```
Input: John has 033 apples and 02 strawberries
Output:
John has 033 apples and 02 strawberries
The sum is 0
Please bear in mind that, although I am familiar with strings, I shouldn't really use them for this solution (not that I'd know how to anyway). 'current' and 'last' are meant to define the current char and the last char from the input. I don't really understand how can I 'construct' my number from the characters, since I don't have to add them digit by digit but as whole numbers. Could you help me? 2. > n = 'current'- 0;
Why the single quotes?

C++ Shell 3. Originally Posted by Salem > n = 'current'- 0;
Why the single quotes?

C++ Shell
Yeah, that was a mistake, I edited the code: n = current - '0'; But the problem remains the same 4. while(isdigit(current))
Isn't going to exit, because current never changes. 5. ## Code:
```#include <stdio.h>
#include <string.h>

#define  true 1
#define false 0

int i, j, x, y, c, r, isOctal = false ;
char currentChar, octalDelim ;
float totalValue = 0.0 ;

void retrieveOctals( char *fromData ) { i = 0 ;
while ( i < strlen(fromData) ) {
currentChar = fromData[i] ;
switch (currentChar) {
case ' ':
isOctal = false ;
if (octalDelim[c-1] != ',')
octalDelim[c++] = ',' ;
if ( fromData[i+1] == '0' )
isOctal = true ;
break;
default:
if ( isOctal ) octalDelim[c++] = fromData[i];
break;
}; i++;
}
}

int main( int args, char * argv[] ) {

char *testInput = "only 064536 octals 345 no 05673 numbers";
retrieveOctals( testInput );
printf( "\n%s\n", octalDelim );

return 0;
}``` 6. Another small thing... In the comments you said:

"Write a function that reads data from standard input until EOF is found and returns the sum of the base-8 numbers found into the input."

So, everything else in the input should be ignored. 7. Notice you can use getline() [POSIX] to get a line of any size (dynamically allocated), strtok() to isolate "pieces" of the line and strtoul() to try to convert strings from octal to unsigned long int... Popular pages Recent additions current, found, input, numbers, sum 