I have declared an array b[3]
and although I don't use it anywhere in the program, the program does not work correctly without it. It only prints out whatever bites the user's previous creation or whatever gets bitten by the user's previous creation. I don't know why this happens or how to make the program run correctly without b[3]
. Here is my code:
#include <stdio.h>
#include <string.h>
main() {
char a[100]="man";
char b[3];
printf ("A man is bitten by a spider and becomes spiderman.\n"
"So in this motive start making stuff.\nTo end the program "
"just click X on top right of the window\n\n");
printf ("When you create sth really big, you go back to only having "
"a man.\n\n");
unsigned char f=1,x=3,i;
while(1){
printf ("You have a %s\n",a);
/*presentation of the menu*/
printf ("Press:\n"
"\t1 for what you have to bite a spider.\n"
"\t2 for what you have to bite a man.\n"
"\t3 for what you have to bite spiderman.\n"
"\t4 for what you have to get bitten by a spider.\n"
"\t5 for what you have to get bitten by a man.\n"
"\t6 for what you have to get bitten by spiderman.\n"
"\t0 to go from the start\n");
scanf("%hhu",&f);
switch(f){
case 0:
strcpy(a,"man");
x=2;
break;
case 1:
x+=6;
if(x<=100){
strcat(a,"spider");
}
break;
case 2:
x+=3;
if(x<=100){
strcat(a,"man");
}
break;
case 3:
x+=9;
if(x<=100){
strcat(a,"spiderman");
}
break;
case 4:
x+=7;
if(x<=100){
for(i=x;i>=6;i--){
a[i]=a[i-6];
}
a[0]='s';
a[1]='p';
a[2]='i';
a[3]='d';
a[4]='e';
a[5]='r';
}
break;
case 5:
x+=4;
if(x<=100){
for(i=x;i>=3;i--){
a[i]=a[i-3];
}
a[0]='m';
a[1]='a';
a[2]='n';
}
break;
case 6:
x+=10;
if(x<=100){
for(i=x;i>=9;i--){
a[i]=a[i-9];
}
a[0]='s';
a[1]='p';
a[2]='i';
a[3]='d';
a[4]='e';
a[5]='r';
a[6]='m';
a[7]='a';
a[8]='n';
}
break;
}
if(x>100){
strcpy(a,"man");
x=2;
}
}
}
Input:
1
Output (with b[3]
):
You have a manspider
Output (without b[3]
):
You have a spider