I didnt even ask to print double quotes. But it keeps printing after I put an input starting with D. When i put 'C', the output is normal. Example:
input > D
2
9
egdqncekq
B4 i put "egdqncekq", the program prints a double quotes for some reason.
#include <stdio.h>
int main() {
char cd, m;
int size, i, disp, m1;
cd=getchar();
scanf("%d", &disp);
scanf("%d", &size);
switch(cd)
{
case 'C':
for(i=0; i<=size; i )
{
m=getchar();
m1=m disp;
if(m1>122)
{
m1=m1-26;
}
printf("%c", m1);
}
break;
case 'D':
for(i=0; i<=size; i )
{
m=getchar();
m1=m-disp;
if(m1<97)
{
m1=m1 26;
}
printf("%c", m1);
}
break;
}
printf("\n");
return 0;
}
CodePudding user response:
After the second scanf
there's a newline character (ASCII 10) left in the input buffer. That newline gets picked up by the getchar
function in each of the loops.
In the "C" case, 2 is added to this value which results in 12. This is the ASCII form feed character which prints as an extra newline.
In the "D" case, 2 is subtracted from this value resulting in 8. This value is less than 97 so 26 is added resulting in 34, which is the ASCII code for the "
character which then gets printed.
Before entering the switch
statement, you should call getchar
in a loop until you see a newline. That will flush out the input buffer.