+ 4

program in c++ to get 10 numbers at runtime and store all even number in one file and negative number in other file?

this is related to working with files in c++

7th Apr 2017, 8:55 AM
shalini
shalini - avatar
8 Answers
+ 13
#include <iostream> #include <fstream> int main() { std::ofstream even; std::ofstream negative; int num; even.open("even.txt"); negative.open("negative.txt"); for (int i = 0; i < 10; i++) { std::cin >> num; if (num < 0) { negative << num << std::endl; } else if (num % 2 == 0) { even << num << std::endl; } } even.close(); negative.close(); return 0; }
7th Apr 2017, 10:23 AM
Hatsy Rei
Hatsy Rei - avatar
+ 12
Please wait while I check the code.
7th Apr 2017, 10:59 AM
Hatsy Rei
Hatsy Rei - avatar
+ 12
You run this on desktop compiler. You compile the code, and your .exe program should be next to your .cpp source code. Just create new text file named "even" and "negative" in the same folder, and run the program. Heck, based on my understanding, the program should be able to create those files by itself if no file is found, so you don't need to do anything. Just run the program on desktop.
7th Apr 2017, 11:10 AM
Hatsy Rei
Hatsy Rei - avatar
+ 12
You are welcomed. :>
7th Apr 2017, 11:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 11
I have updated the answer. Sorry for the errors, I'm still getting used to not using namespace std...
7th Apr 2017, 11:01 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
thanks
7th Apr 2017, 11:01 AM
shalini
shalini - avatar
+ 3
hey the first program is also correct with little mistake..after doing certian changes i got the output.thanks
7th Apr 2017, 11:17 AM
shalini
shalini - avatar
+ 1
hey the number which is negative can be even number so the number have to store on even file..how it can be done
7th Apr 2017, 11:21 AM
shalini
shalini - avatar