Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i used %c in scanf("%c",y)1 in my code. it didnt work. but when i replaced %c with %s. it worked. why? please answer asap. i have exam tomorrow and i need to defend it.
heres my entire code.

C++
#include<stdio.h>
#include<conio.h>

void main(){
int x; //sport
char y;//role,event,style,etc
clrscr();
printf("enter a number between 1-5:");
scanf("%d",&x);
if(x==1){
	printf("basketball\n");
	printf("enter a or b:");
	scanf("%s",&y);
	if(y=='A'||y=='a'){
	printf("forward");
	}
	else if(y=='B'||y=='b'){
	printf("guard");
	}
	else{
	printf("invalid input");
	}
}
else if(x==2){
	printf("badminton\n");
	printf("enter a or b:");
	scanf("%s",&y);
	if(y=='A'||y=='a'){
	printf("singles");
	}
	else if(y=='B'||y=='b'){
	printf("doubles");
	}
	else{
	printf("invalid input");
	}
}
else if(x==3){
	printf("baseball\n");
	printf("enter a or b:");
	scanf("%s",&y);
	if(y=='A'||y=='a'){
	printf("catcher");
	}
	else if(y=='B'||y=='b'){
	printf("pitcher");
	}
	else{
	printf("invalid input");
	}
}
else if(x==4){
	printf("swimming\n");
	printf("enter a or b:");
	scanf("%s",&y);
	if(y=='A'||y=='a'){
	printf("back stroke");
	}
	else if(y=='B'||y=='b'){
	printf("butterfly");
	}
	else{
	printf("invalid input");
	}
}
else if(x==5){
	printf("track and field\n");
	printf("enter a or b:");
	scanf("%s",&y);
	if(y=='A'||y=='a'){
	printf("discus throw");
	}
	else if(y=='B'||y=='b'){
	printf("100m dash");
	}
	else{
	printf("invalid input");
	}
}
else{
	printf("invalid input");
}
getch();
}
Posted
Updated 9-Sep-15 7:01am
v2
Comments
PIEBALDconsult 9-Sep-15 13:10pm    
"If you have to include code, include the smallest snippet of code you can." You would do well to write a very small test app that demonstrates the problem.
Also, I recommend that you write a function that handles input rather than repeating the code as you do -- it reduces the possibility of bugs and makes maintenance easier.
Member 11971871 9-Sep-15 13:28pm    
thanks! i just started coding. i still dont know how to create a function. my only problem is the compiler doesnt read the scanf()... line inside the if(x==1), and after the skipping, it automatically execute the statement in else (inside the if(x==1)). the problem occurs while using %c.

1 solution

You should never use
C
scanf("%s", &y);

if y is declared as char.

You should always check scanf function result (it should be 1 in your case).

You have to consider that console input is buffered, that is, scanf doesn't return until you press return.

Please consider posting more meaningful info, "It doesn't work" is really poor.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Sep-15 13:23pm    
Absolutely, a 5.
—SA
CPallini 9-Sep-15 15:34pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900