After I Prompt the user to get an integer from the keyboard, display the number on screen if it is an even number how do I write the number into a file in A:\ drive under the name OddFile.txt. if its odd.

#include <iostream>                                                                                      
#include <fstream>    
#include <cstdlib>    
using namespace std;    
 int main ()    
{    
    int num;    
     ifstream AFile;      
     AFile.open("a:\\OddFile.txt");    
     cout << "Enter an integer number please: ";    
     cin >> num;     
     if (num % 2 == 0)    
            cout << num << " is even\n";    
     else    
          AFile.open("a:\\OddFile.txt"); 
           
           AFile.close();
           
      system("pause");       
       return 0;    
}

Recommended Answers

All 2 Replies

You have opened the file twice, I think you only need to open it once as
you did first.
So if you want to write the number to the file you could do like this:

ofstream AFile;
AFile.open("a:\\OddFile.txt");

AFile << num;
AFile.Close();

line 15 is still wrong -- what is it that you want to do when the value of num is odd ? I know you don't want to open the output file because you already did that at line 9. Someone else already gave you a hint of what to do in your other thread about this same program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.