Thread: Sum of octals found in input

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    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?
    Last edited by rmmstn; 11-26-2020 at 07:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > n = 'current'- 0;
    Why the single quotes?

    Why doesn't your compiler complain?
    C++ Shell
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    Quote Originally Posted by Salem View Post
    > n = 'current'- 0;
    Why the single quotes?

    Why doesn't your compiler complain?
    C++ Shell
    Yeah, that was a mistake, I edited the code: n = current - '0'; But the problem remains the same

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    while(isdigit(current))
    Isn't going to exit, because current never changes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    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[1024] ;
    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;
    }
    "without goto we would be wtf'd"

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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 subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-22-2020, 05:17 PM
  2. comparing octals???
    By TaiL in forum C Programming
    Replies: 1
    Last Post: 10-13-2009, 12:55 PM
  3. Pq - Sq - Kq --- Found!!!
    By sean in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-02-2002, 07:07 PM
  4. Look what I found!
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-14-2002, 05:34 AM
  5. -lqt not found?
    By f0d in forum Linux Programming
    Replies: 2
    Last Post: 06-03-2002, 10:17 AM

Tags for this Thread