0
Day:
                                printf("\n1:Mon\t2:Tue\t3:Wed\t4:Thu\n5:Fri\t6:Sat\t7:Sun\nEnter value-> ");
                                scanf("%d",&x);
                                char dd1[5]="MON"; char dd2[5]="TUE"; char dd3[5]="WED";
                                char dd4[5]="THU"; char dd5[5]="FRI"; char dd6[5]="SAT";
                                char dd7[5]="SUN";
                                if(x==1){  strcpy(dd,dd1);   } 
                                if(x==2){  strcpy(dd,dd2);   }    
                                if(x==3){  strcpy(dd,dd3);   } 
                                if(x==4){  strcpy(dd,dd4);   } 
                                if(x==5){  strcpy(dd,dd5);   } 
                                if(x==6){  strcpy(dd,dd6);   }
                                if(x==7){  strcpy(dd,dd7);   } //printf("%s",dd);
                                if(x>7 || x<1)
                                {
                                       cout<<"\nPlease Enter Valid Option\n";
                                       goto Day ;
                                }

this is part of my code.....
If i given input any character it goes infinite loop...
E.g:- q
if gives int value it works fine
how to resolve it??

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by mitrmkar
0
Day:
                                printf("\n1:Mon\t2:Tue\t3:Wed\t4:Thu\n5:Fri\t6:Sat\t7:Sun\nEnter value-> ");
                                scanf("%d",&x);
                                char dd1[5]="MON"; char dd2[5]="TUE"; char dd3[5]="WED";
                                char dd4[5]="THU"; char dd5[5]="FRI"; char dd6[5]="SAT";
                                char dd7[5]="SUN";
                                if(x==1){  strcpy(dd,dd1);   } 
                                if(x==2){  strcpy(dd,dd2);   }    
                                if(x==3){  strcpy(dd,dd3);   } 
                                if(x==4){  strcpy(dd,dd4);   } 
                                if(x==5){  strcpy(dd,dd5);   } 
                                if(x==6){  strcpy(dd,dd6);   }
                                if(x==7){  strcpy(dd,dd7);   } //printf("%s",dd);
                                if(x>7 || x<1)
                                {
                                       cout<<"\nPlease Enter Valid Option\n";
                                       goto Day ;
                                }

this is part of my code.....
If i given input any character it goes infinite loop...
E.g:- q
if gives int value it works fine
how to resolve it??

You need to check the return value of scanf() which tells you whether it succeeded or not (in getting an int from the user).

scanf()

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.