help ,i am running in c++,it says string is not declare,and i wanted to use only these header file,not iostream | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

help ,i am running in c++,it says string is not declare,and i wanted to use only these header file,not iostream

#include <stdio.h> #include<string.h> int main() { string s; scanf("%s",s); printf("%s",s); return 0; }

8th Jun 2018, 8:46 AM
Bahubali
Bahubali - avatar
3 Réponses
+ 3
string keyword is not present in c (stdio.h headers) use a character array instead eg: char s[20];
8th Jun 2018, 8:49 AM
‎ ‏‏‎Anonymous Guy
+ 2
you can use printf("%s", s.c_str()), but scanf can only deal with char arrays and s.c_str() wont work because it returns a const char * and points to an empty char array. so in the end you have to use Sreejith‘s answer if you want to use scanf. the answer to this stackoverflow post also has a solution https://stackoverflow.com/questions/20165954/read-c-string-with-scanf
8th Jun 2018, 8:52 AM
Max
Max - avatar
0
ok thanks Sreejith and Max
8th Jun 2018, 9:54 AM
Bahubali
Bahubali - avatar