0

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;    
}
3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

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();

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.