Answer to Question #181192 in C++ for Kanav Garg

Question #181192

A set of numbers is stored in a file named numbers.txt. Write a program to copy all odd numbers to another file called odd.txt and all even numbers to a file called even.txt.

 


1
Expert's answer
2021-04-14T16:03:57-0400
#include<bits/stdc++.h>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
    int x=0;
    ifstream fin;
    ofstream fout1("odd.txt"),fout2("even.txt");
    fin.open("numbers.txt");
    string s;
    while(!fin.eof())
    {
        fin>>s;
        x=stoi(s);


        if(x%2==0)
        {
            fout2<<x<<" ";
        }
        else
        {
            fout1<<x<<" ";
        }
    }
    fin.close();
    fout1.close();
    fout2.close();
}

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