0

what is wrong in this i am unable to give the input can anyone please rectify

#include<stdio.h> #include<string.h> int main() { int key,i; char a[20]; printf("enter the key"); scanf("%d",&key); printf("enter the string "); gets(a); for(i=0;i<strlen(a);i++) { if((a[i]=='-')||(a[i]==' ')) { continue; } else if((a[i]>=48)&&(a[i]<=57)) { a[i]=a[i]+key; } else { a[i]=a[i]-key; } } printf("\n elements are "); puts(a); }

24th Oct 2020, 6:59 PM
mohana hemanth
mohana hemanth - avatar
6 Réponses
+ 4
The problem is that after the key has been entered, the newline character will remain in the input buffer. When gets() starts reading in characters, it will first encounter that newline character, however, gets() is designed to stop reading in characters after encountering a newline character. That means the rest of the input will remain in the input buffer and won't be placed in 'a'. To avoid this, you can replace scanf( "%d", &key ) with scanf( "%d\n", &key ), which will remove the newline character from the input buffer. Note: It is strongly advised to use fgets() or scanf() instead of gets(): https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used
24th Oct 2020, 7:23 PM
Shadow
Shadow - avatar
+ 3
Well, as I already said earlier in my first answer, you can fix the problem by replacing scanf( "%d", &key ); with scanf( "%d\n", &key ); and be done with it. I don't think you need me to do that, regardless of experience level. However, I can also see that the surrounding explanation might be overwhelming at first. If you were to communicate what about it you did not understand, I could try to revise my answer and give a better explanation of the fundamental issue. Just in case, here are some links to threads dealing with the same problem: https://stackoverflow.com/questions/34023682/c-cannot-read-string-after-i-read-an-integer https://www.geeksforgeeks.org/problem-with-scanf-when-there-is-fgetsgetsscanf-after-it/ http://c-faq.com/stdio/scanfinterlace.html
25th Oct 2020, 3:52 PM
Shadow
Shadow - avatar
+ 2
I really don't quite see what prevents you from making the changes yourself? I told you exactly what to do, and it's literally only two characters, maybe one additional line if you replace gets() as well. Or was the explanation insufficient?
24th Oct 2020, 10:58 PM
Shadow
Shadow - avatar
+ 1
the question for this program is we have to give the statement it can be numbers or string which have to be stepped by key value (i.e., if key is 1 and the string is "how are you" then it should give output as "gnv zqd xnt" similarly if the number is given then it should increament by key value).
24th Oct 2020, 7:06 PM
mohana hemanth
mohana hemanth - avatar
0
Shadow can u change the code and give me
24th Oct 2020, 8:31 PM
mohana hemanth
mohana hemanth - avatar
0
Shadow may be u said the right answer but i am still novice it is like greek and latine for me actually i started learning since 1 month.
25th Oct 2020, 4:09 AM
mohana hemanth
mohana hemanth - avatar