Heya can u help me. I have a problem in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Heya can u help me. I have a problem in c

code is #include<stdio.h> # include<conio.h> int main() { char str[10][10]; int i,n; print("enter no. of students"); scanf("℅d",&n); for(i=0; i<n; i++) { printf("enter name of student : %d" i+1): gets(str[i]); } printf ("student names are"); for(i=0; i<n;i++) { puts(str[i]); } getch(); return 0; } I m getting enter name of student 0 and enter name of student 1 by default why plz help

19th Feb 2017, 10:47 AM
AYUSH SHARMA
AYUSH SHARMA - avatar
1 Answer
+ 3
#include <stdio.h> int main (void){ char str[10][10]; int i, n; printf("Enter no of students: "); scanf("%d",&n); /* When number are fed into by keyboard, number are stored in the memory location addressed by the variable and the return key remains in the keyboard buffer It is transferred to the the string variable next in the line. That's why you are facing problem. Make use of fflush (stdin) to clear the keyboard buffer. */ fflush(stdin); for (i=0;i<n;i++){ printf("Enter the name of the student #%d: ", (i+1)); gets(str[i]); } printf ("Student Names are: \n"); for (i=0;i<n;i++){ puts(str[i]); } return 0; }
19th Feb 2017, 11:07 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar