Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #define INPUT_FILE "input.txt"
  5. #define OUTPUT_FILE "output.txt"
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. bool end = false;
  11.  
  12. int N;
  13. int y;
  14.  
  15. fstream infile, outfile;
  16.  
  17. infile.open(INPUT_FILE, fstream::in);
  18. outfile.open(OUTPUT_FILE, fstream::out);
  19.  
  20. if (!infile.is_open()) {
  21. cerr << "Error opening file " << INPUT_FILE << endl;
  22. return -1;
  23. }
  24.  
  25. if(!outfile.is_open()){
  26. cerr << "Error opening file " << OUTPUT_FILE << endl;
  27. return -1;
  28. }
  29.  
  30. while (!end) {
  31. infile >> N;
  32.  
  33. if(infile.eof()){
  34. cout << "End of input, exit! " << endl;
  35. end=true;
  36. }
  37.  
  38. else if(infile.fail()){
  39. cerr << "Error reading N from file " << INPUT_FILE << endl;
  40. end=true;
  41. }
  42.  
  43. else {
  44. int v[N];
  45. int i;
  46. for (i=0; i<N; i++) {
  47. infile >> y;
  48. if (y%2==1)
  49. v[i]=y;
  50. else
  51. infile.ignore();
  52.  
  53.  
  54. }
  55. for (i=0; i<N; i++) {
  56. if (!(outfile << v[i] << " ")) {
  57. cerr << "error writing number on file!" << endl;
  58. end = true;
  59. }
  60. }
  61.  
  62. if (!(outfile << "\n")) {
  63. cerr << "error writing on file!" << endl;
  64. end = true;
  65. }
  66.  
  67.  
  68. }
  69. }
  70. infile.close();
  71. outfile.close();
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement