What's the wrong in this code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What's the wrong in this code ?

#include<stdio.h> int main() { int roll; printf("Welcome To This Page"); printf("\n enter the roll number = "); scanf("%d",&roll); switch(roll) { case'1': printf("Name = Abhishek kumar Roll no =1 branch= CS"); break; case'2': printf("Name = Abhinav Raj roll no = 2 Branch Cs"); break; case'3': printf("Name = Atul kumar Rol number = 3 Branch = cs"); break; default: printf("Something went wrong"); } return 0; }

3rd Jun 2020, 1:35 AM
Abhishek Kumar
Abhishek Kumar - avatar
2 Answers
+ 5
roll is of type int but your cases are of type char. They will only be triggered when roll matches their ascii values: '1' = 49, '2' = 50, '3' = 51, etc. Remove the single quotes or declare roll as char and it will work.
3rd Jun 2020, 1:44 AM
Gen2oo
Gen2oo - avatar
0
Thanks
3rd Jun 2020, 1:55 AM
Abhishek Kumar
Abhishek Kumar - avatar