Writing to a file

Posted on 2005-05-11
Medium Priority
Last Modified: 2010-04-01
Hey everyone, I have a file containing numerical values ie:


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;    

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

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

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

   return 0;

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

Question by:Helix
1 Comment
LVL 37

Accepted Solution

Harisha M G earned 240 total points
ID: 13981105
Hi Helix,

Remove this part of the code...

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


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question