not reading the string value..
Title
Question
#include<stdio.h>
//#include<string.h>
void main()
{
int n;
char str[20]; //={"am string"};
printf("Enter a number");
scanf("%d",&n);
printf("Enter a String");
scanf("%[^\\n]s",str);
printf("The Number You entered is: %d\\n",n);
printf("The String You entered is: %s\\n",str);
}
//#include<string.h>
void main()
{
int n;
char str[20]; //={"am string"};
printf("Enter a number");
scanf("%d",&n);
printf("Enter a String");
scanf("%[^\\n]s",str);
printf("The Number You entered is: %d\\n",n);
printf("The String You entered is: %s\\n",str);
}
C-and-Cpp Strings 04-05 min 0-10 sec
Answers:
You should do scanf("%s", str) to read a string
scanf("%d",&n);
fflush(stdin);
printf("Enter a String");
After entering number n when you pressed ENTER key it is taken as String Input So you are not able to enter string value on the output screen.
The solution: To clean the standard input stream use fflush(stdin).
In line 2, // is added. Hence, "string.h" is not included in file, instead it had became comment.
Login to add comment