0

The compilation thing!!

Why does the following code outputs compilation error:#include <iostream> #include <fstream> using namespace std; int main (){ ofstream MyFile; MyFile.open("num.txt"); int x,y; cin>>x; cin>>y; MyFile << x+y << endl; ifstream MyFile1("num.txt"); int sum; while (getline (MyFile1,sum)){ MyFile1 << sum << endl; } }

26th Jan 2017, 4:24 PM
Emanuel Dajti
Emanuel Dajti - avatar
3 Respostas
+ 5
Next time, please include your compile error code here. Your ifstream operators are wrong. Use >> instead of <<, and there is no endl for ifstream. Chances also are that you did not close your file before opening the same file with ifstream. For the love of cookies, please write all your declarations right after starting main() instead of declaring them right before you need them.
26th Jan 2017, 5:10 PM
Hatsy Rei
Hatsy Rei - avatar
0
You can't use cin without #include <iostream> You can't work with files without #include <fstream> I am not that good at C++ but I would never make this mistake.
26th Jan 2017, 4:39 PM
Dawzy
Dawzy - avatar
0
God!! LOL i have included both see carefully!
26th Jan 2017, 4:41 PM
Emanuel Dajti
Emanuel Dajti - avatar