+ 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++
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;
}
+ 12
Please wait while I check the code.
+ 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.
+ 12
You are welcomed. :>
+ 11
I have updated the answer. Sorry for the errors, I'm still getting used to not using namespace std...
+ 3
thanks
+ 3
hey the first program is also correct with little mistake..after doing certian changes i got the output.thanks
+ 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