Answer to Question #218837 in C++ for Hemambar

Question #218837

Write a program to create a numeric file and split the file into two files, one containing all odd

numbers and another with even numbers.



1
Expert's answer
2021-07-23T17:31:24-0400
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void WriteFile()
{
    string data, numbers = "0123456789";
    ofstream dataFile("data.txt");
    cout << "Enter all the numeric data: ";
    getline(cin, data);
    for (int i = 0; i < data.length(); ++i)
    {
        if (numbers.find(data[i]) != string::npos)
        {
            dataFile << data[i];
        }
    }
}

void SplitFile()
{
    string data;
    ifstream dataFile("data.txt");
    ofstream even("even.txt");
    ofstream odd("odd.txt");
    dataFile >> data;
    for (int i = 0; i < data.length(); ++i)
    {
        if (data[i] % 2 == 0)
        {
            even << data[i];
        }
        else
        {
            odd << data[i];
        }
    }
}

int main()
{
    WriteFile();
    SplitFile();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS