# Thread: while function with scanf function confusion

1. ## 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;
}``` 2. 3. 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. 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. 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). 6. 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. 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. Originally Posted by yukapuka when does the while loop test become 0?
Usually never when just scanning for chars. Read the doc.
Kurt Popular pages Recent additions 