Thread: while function with scanf function confusion

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    while function with scanf function confusion

    Hi this is my code below, now whats confusing me much is the while(scanf) part

    i havent completed the function i am trying to implore, but what this program is supposed to do is descrypt or encrypt very simply some letters

    what problem i am having is this, when the while runs the function scanf("%c",encode(ch)))

    it is supposed to take all the letters that is input and do with them accordingly, the shift integer determines if they will be scrambled down or up, so if shift is less than 0 it will take the letter and according to what shift is set to it will replaces that may times down from the original letter with this letter n times away governed by the shift variable

    now again the problem i am having is with the encode ch function i wrote, i dont know what scanf is supposed to do with it, does it return negative when what happens? when is it supposed to end?

    Code:
    #include <stdio.h>
    
    int rotate_right(int);
    int rotate_left(int);
    int encode( int, int);
    
    int main(void)
    {
        int ch=0,shift,n;
        
        printf("Enter a number for amount of rotation: ");
        scanf("%d",&shift);
        
        while(scanf("%c",encode(ch, shift)))
        printf("%c",ch);    
      
       
        return 0;
        
    }
    
    int encode(int ch, int shift){
        
        while(shift!=0){
        if(shift>0){
            rotate_right(ch);
        shift-=1;
        }
        else if(shift<0){
            rotate_left(ch);
            shift+=1;
        }
        }
        
        return ch;
    }
    
    int rotate_right(int ch){
        
        if((ch >= 'A'&& ch < 'Z')||(ch >= 'a'&& ch < 'z'))
        ch++;
      else if(ch == 'Z')
        ch = 'A';
      else if(ch == 'z')
        ch = 'a';
        
        return ch;
    }
    int rotate_left(int ch){
        
      if((ch > 'A'&& ch <= 'Z')||(ch > 'a'&& ch <= 'z'))
        ch--;
      else if(ch == 'A')
        ch = 'Z';
      else if(ch == 'a')
        ch = 'z';
      
       return ch;
    }
    Last edited by yukapuka; 08-08-2012 at 03:28 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    48
    okay thank for that very inspiring and enthusiastic help, but i still cant work this out so if you dont mind helping me, the code is above

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    48
    my problem is that i dont know how to deal with the encode function here, it doesnt do its job in the while loop

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Do you have a debugger available such as gdb? This is a great time to learn how to use it. Step through the code and verify that the values in your variables (shift and ch) are what you expect them to be. I suspect they are not in at least a couple of places, specifically:

    1) You cannot change shift inside your encode function, unless you pass it as a pointer and call the function using the address of your main() shift variable. Right now your encode shift variable is a CLONE of your main() shift variable and any changes you make to it are simply just made to the local copy rather than what you perceive to be the "global" shift.

    2) You are passing ch to encode but never using it and simply returning it back at the end of the function. That accomplishes nothing.

    3) Your rotate functions return ch, you could return their return value from encode to address point 2).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Additionally to what claudiu said:
    Code:
    while(scanf("%c",encode(ch, shift)))
    Passing the char returned from a function to scanf() is completely wrong.
    "%c" expects a pointer to char.
    Also look up what scanf() returns.
    Kurt

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    48
    Thanks for that
    now my question is this: what happens to inputs that are scanned by scanf? do they get put into ch which then get passed to the function encode
    also after encode does with them and processes them through rotate, and they get returned back to encode, does encode then return them back to scanf? and does that make the while funcion non zero? when does the while loop test become 0? is it when i press enter into input?(because that does happen when i run it)

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by yukapuka View Post
    when does the while loop test become 0?
    Usually never when just scanning for chars. Read the doc.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  2. confusion over use of a function
    By flyingants in forum C Programming
    Replies: 8
    Last Post: 11-12-2010, 02:26 PM
  3. Elevator Function confusion
    By applescruff in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2004, 10:14 AM
  4. Recursive function confusion
    By Sereby in forum C Programming
    Replies: 6
    Last Post: 07-22-2004, 12:45 PM
  5. Newbie function confusion...
    By funkydude9 in forum C++ Programming
    Replies: 9
    Last Post: 12-15-2002, 02:15 AM