This program is meant to apply rot13 to input. However, when I compile I get warnings that lines 20, 23, 29 have no effect in code and line 17 is unreachable code. When anything is entered, it just continues the program 开发者_运维技巧and leaves a blank space. What's wrong?
int main()
{
int c;
while ( ( c = getchar() ) != EOF )
putchar( r13( c ) );
return 0;
}
int r13(int c)
{
if(( tolower( c ) >='a' && c <='z'))
{
if((c) < 'm')
(c = c + 13);
else
{
(c = c - 13);
}
}
else
c = c;
return ( isalpha( c ) );
}
EDIT: I stupidly put return there without noticing, which fixes the unreachable error. But I still hhave no idea why c+13 and c-13 have no effect?
EDIT: I wanted to create a program in C that was a rot13 encoder/decoder. This is the original code and works fine. However I wanted to convert to modulus design, and my lack of knowledge has led me to failing. Hope this helps people understand what I'm trying to do.
int main(void)
{
int c,e;
while((c=getchar())!=EOF)
{
if(c >='A' && c <='Z')
{
if((e = c + ROT) <= 'Z')
putchar(e);
else
{
e = c - ROT;
putchar(e);
}
}
else if(c >='a' && c <='z')
{
if((e= c + ROT) <= 'z')
putchar(e);
else
{
e = c - ROT;
putchar(e);
}
}
else
putchar(c);
}
return 0;
}
EDIT: ok finally fixed it, so many silly errors, thank you for help. Also the program should ask the user if they wish to input another value and, if so, repeat the process. There are so many ways of doing this, just wondering what you guys think is the best way of doing it.
Yes they don't have an effect. Hard to guess what you meant but are you just missing the return
keywords before them?
You need c = c + 13;
or c = c - 13;
to change the value of c.
To return the value of c you need return c;
What are you expecting (c + 13);
to do? This just evaluates...but does nothing. It won't modify c
. In order to modify c
to contain the value c + 13
you need to write c = c + 13;
Keep in mind that you've declared c as an "int" and not a "char".
Also, what's up with the
else
c;
What is that supposed to do?
I think you would benefit from studying up on the basics of programming. In particular, =
is an assignment operator. The way it works is that the expression on the right of the =
is "assigned" to the variable (or const) on the left. So, in order to increment c by 13, you would need to write c = c + 13;
This evaluates c + 13
and then sticks that value inside of c
.
Another key concept is return
inside of functions. Returning a value ends the function and provides that value to whatever was calling the function. Remember that the computer executes everything in sequence, so if you stick a return statement at the beginning of your function, the rest of the code will not execute.
you are retuning before if()
int r13(int c)
{
return ( isalpha( c ) ); //put it last line of your function
if(( tolower( c ) >='a' && c <='z')) //this will be unreachable
.....
}
Explanation:
return
statement will send back the flow of control without executing code below return
statement when your r13(int c)
function is called.
Your code is... weird...
Any code after line 13 can not be reached as you return isalpha(c)
. Compiler is right.
You should also give code aesthetics more attention (ugly code may lead to this kind of bugs).
The return statement at the star of your r13 function effectively makes all the rest of the code in the function "dead code".
精彩评论