+ 4
What is alternative of cin.ignore(); (used in C++) in C
#include <stdio.h> int main() { char ch,s[100],sen[200]; scanf("%c",&ch);//to input a character scanf("%s",&s);//to input a string gets(sen);//to input a sentence printf("%c\n",ch);//to print character printf("%s\n",s);//to print string printf("%s",sen);//to print sentence return 0; } In this Code memory of sen remains empty i.e value inputed for sen is not being stored in it. So what can be done to store it. As scanf("%s", &sen); terminates on pressing space or enter
3 ответов
+ 4
Thanks for your review Omkar
But the space after format specifier also terminates as the space appears.
I think I found an Alerternative of cin.ignore() in C
checkout here :
https://code.sololearn.com/coQZZxSUAMLJ/?ref=app
+ 3
Mayank Matwa
Nice solution.
However using space will also work.
You may have misplaced space.
You should give space between ending quote and 's' of format specifier
scanf("%s ",s);
//one more thing that you don't need a & because array name itself is pointer to first element.
+ 2
Mayank Matwa
There is no alternative for cin.ignore(); in C according to SO discussion:
https://stackoverflow.com/questions/53037333/whats-the-equivalent-of-cin-ignore-in-c
But using a space after format specifier will solve the problem.
scanf("%s ",s);// see the space after format specifier