0

Why am I getting an error here? (C++/C)

Code: #include <iostream> #include <fstream> #include <cstring> using std::cout; using std::endl; int main() { std::fstream fileIO; fileIO.open("out.txt", std::ios::app); if (fileIO.is_open()) { char* str; scanf("%s", str); while (strcmp(str, "") != 0) { fileIO << str << "\n"; scanf("%s", str); } } else cout << "Couldn't open file :(" << endl; fileIO.close(); return 0; }

5th Feb 2023, 1:40 PM
Yahel
Yahel - avatar
2 Respostas
0
where are you running that code? pointer str is passed to scanf without initializing it . it has to point to some location on memory for scanf to store data there.
5th Feb 2023, 5:09 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Bahha🐧 I'm running that code on Repl.it. Thanks for the answer, I understand now that I need to either allocate on the heap via malloc/new or type: char str[some bytes].
5th Feb 2023, 6:06 PM
Yahel
Yahel - avatar