0

So I'm following my professors powerpoint and I think what I'm doing is correct, the issue I am having is when I execute my program I am unable to input anything. Image at the bottom "The input file should have some integer values (e.g., 10 values) separated by whitespaces."

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream myfile;
ofstream newfile;
myfile.open("test.txt");
newfile.open("new_test.txt");
cout <<"test.txt is opened to get input from it"<<endl;
cout <<"new_test.txt is opened to write in it"<<endl;

int mynum;
while(!myfile.eof())//finding whether end of file is reached
{
    myfile>>mynum;
    newfile<<mynum<<endl;
}
myfile.close();
newfile.close();

return 0;
}

Image

My program isn't letting me input any letter/numbers or anything else for that matter.

  • 3
    Possible duplicate of [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Ron Oct 14 '18 at 16:30
  • You probably have your text file in the wrong location. Or perhaps it has 2 to .txt extensions because windows confused you by hiding the extension by default. Also please read the link from @Ron your code does contain the bug he mentions. – drescherjm Oct 14 '18 at 16:38
  • 1
    ***I don't have any errors in my program*** Not having any compiler errors does not mean you don't have logical errors. – drescherjm Oct 14 '18 at 16:40
  • BTW, neither of those statements are checked for their truth. You don't actually check if the open was successful in either case. – drescherjm Oct 14 '18 at 16:42
  • `myfile.open´ may fail. You are supposed to check for it. If you don't care whether an operation succeeds or fails, why bother with it at all? – n. 'pronouns' m. Oct 14 '18 at 16:44

0 Answers0