Link to home
Start Free TrialLog in
Avatar of Helix
Helix

asked on

Writing to a file

Hey everyone, I have a file containing numerical values ie:

12
45
19
65

I'm messing around with input and output files. Basically I have my program reading in the file with the above numbers in, and I wish ouput another file stating by "Y" or "N" If the values are above 50.

Here is my code:

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

int main()
{
 
   int num;
   ofstream outfile;
   ifstream infile;    
   infile.open("numbers.txt");
   outfile.open("outputs.txt");

   if (infile.fail())
    {
      cout <<"Error opening file, program closing "<< endl;
      exit(1);
    }
     

while ( infile >> num )
   {
       cout  <<  num << endl;
   }  

   cout <<"Checking numbers"<< endl;
  while ( infile >> num )
   {
     
        if (num < 50)
      outfile << "N"  << "\t" ;  
        else
        outfile << "Y" << "\t";
   }
   
   infile.close();
   outfile.close();


   return 0;
}

Although the program compiles and runs ok, it doesn't output anything to the output file. Can anyone help?

Helix
ASKER CERTIFIED SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial