Thread: Can someone help me with this small piece of code

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Can someone help me with this small piece of code

    Okay, so I have this piece of code here to input a string from user and convert it to a string half of the size. It works for all inputs except for ones with Zeroes in the middle. For example:

    INPUT: 1234
    OUTPUT: 0, 6, 1, 7

    INPUT: 1230
    OUTPUT: 0, 6

    INPUT: 12
    OUTPUT: 0, 6

    INPUT: 10
    OUTPUT: _____ (NOTHING), it doesn't even print the text in the printf statement

    Why would a case with 0's in it be any different for what I am doing here?

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(){
    
    
    char array[256];
    fgets(array,255,stdin);
    
    
    int f;
    for(f=0;f<strlen(array)-1;f++){
    array[f] -= 48;
    }
    
    
    int i;
    
    
    int last_cell = strlen(array)-2;
    
    
    i = last_cell;
    
    
    for(i=last_cell ; i>=0; i--){
    
    
    int flag = 0;
    if( i != 0 && (array[i - 1] % 2) == 1)
    flag = 1;
    
    
    array[i] /= 2;
    if(flag == 1){
    array[i] += 5;
    }
    }
    
    
    int j;
    for(j=0;j<=last_cell;j++){
    printf("array[%d] == %d,",j,array[j]);
    
    
    }
    }
    Last edited by Salem; 04-10-2012 at 10:43 AM. Reason: Restored

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    I think the problem is that when I run the strlen function, it picks up the 0s as a "/0" null terminating character and thus errs in its measurement of the string. How else can I do this. I tried loading the input into the string with a for loop and getchar(), but that doesn't work because I don't know how many values the user will input so I have to loop 255 times to load a string that could be for four or five values.

    Also side question with regard to the previous point, since I already have your attention. Why is it that when I do a for loop to load a string of 256, say

    Code:
    for(i=0;i<256;i++) ;
    array[i] = getchar();
    if(getchar() == '\n') break;
    If I input a five character string, the command line doesn't return, it just stays there waiting, maybe expecting more output?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why don't you turn the user input into an integer, do the arithmetic, and then turn it back to a character array?

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Because I am trying to do this for arbitrarily long integers between 1 and 255. So that was my first plan of action, but at around 24 ints or so the long long int stops holding the conversion value accurately

    Unless that's not what you meant and if so please elaborate if you can.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Once you've subtracted '0' (or 48 as you call it) from all the elements, any that were '0' are now 0 so you can't use any string functions after that point since they interpret 0 as the end of the array. To fix it simply set a variable to the length of the array before modifying the data and use that variable instead of calling strlen.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    NVM Fixed it. I simply moved the last_cell variable assignment up to before my loop converting the string ASCIIs into their decimal values. If anyone cares, haha

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Haha, ooga, I swear I didn't read that before I did it but thanks any way. You are a wise fellow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's wrong with this piece of code?
    By shaxquan in forum C Programming
    Replies: 3
    Last Post: 06-14-2010, 04:46 AM
  2. Bad feeling about piece of code
    By Kurbein in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2009, 12:13 PM
  3. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM
  4. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  5. need help to understand a piece of code
    By rraajjiibb in forum C Programming
    Replies: 3
    Last Post: 08-09-2004, 11:18 PM