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

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; } }

c++

1/26/2017 4:24:13 PM

Emanuel Dajti

3 Answers

New Answer

+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.

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.

0

God!! LOL i have included both see carefully!