The compilation thing!! | Sololearn: Learn to code for FREE!


The compilation thing!!

Why does the following code outputs compilation error:#include <iostream> #include <fstream> using namespace std; int main (){ ofstream MyFile;"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; } }


1/26/2017 4:24:13 PM

Emanuel Dajti

3 Answers

New Answer


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.


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.


God!! LOL i have included both see carefully!