Advertisement
Lawnknome

lab4

Apr 21st, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. void name_file (std::string &input_string1, std::string &input_string2, std::string &output_string);
  7. void sort_input (std::ifstream &input_file1, std::ifstream &input_file2, std::ofstream &output_file);
  8. void set_input_name (std::ifstream &input, std::string input_name);
  9. void set_output_name (std::ofstream &output, std::string output_name);
  10. void close_file_input(std::ifstream &close);
  11. void close_file_output(std::ofstream &close);
  12.  
  13. int main ()
  14. {
  15.     std::vector<int> output_vect;
  16.     std::vector<int> potential_next;
  17.     std::ifstream input_file1, input_file2;
  18.     std::ofstream output_file;
  19.    
  20.     std::string input_filename1;
  21.     std::string input_filename2;
  22.     std::string output_filename;
  23.    
  24.     name_file(input_filename1, input_filename2, output_filename);
  25.     set_output_name(output_file, output_filename);
  26.     set_input_name(input_file1, input_filename1);
  27.     set_input_name(input_file2, input_filename2);
  28.  
  29.     sort_input(input_file1, input_file2, output_file);
  30.  
  31.     close_file_input(input_file1);
  32.     close_file_input(input_file2);
  33.     close_file_output(output_file);
  34.  
  35.     return 0;
  36. }  
  37.  
  38.  
  39. void name_file(std::string &input_string1, std::string &input_string2, std::string &output_string)
  40. {
  41.     //name the ouput file
  42.     std::cout << "Including the file extension, please enter the name of the desired output file: ";
  43.     std::cin >> output_string;
  44.    
  45.     //name the first input file
  46.     std::cout << "What is the name of the first input file?  Please include the file extension: ";
  47.     std::cin >> input_string1;
  48.    
  49.     //name the second input file
  50.     std::cout << "What is the name of the second input file?  Please include the file extension: ";
  51.     std::cin >> input_string2;
  52. }
  53.  
  54. void set_input_name (std::ifstream &input, std::string input_name)
  55. {
  56.     //open file
  57.     input.open(input_name.c_str());
  58.    
  59.     if (input.fail())
  60.     {
  61.         std::cout << "Error opening input file " << input_name << "." << std::endl;
  62.     }  
  63. }
  64.  
  65. void set_output_name (std::ofstream &output, std::string output_name)
  66. {
  67.     //open file
  68.     output.open(output_name.c_str(), std::ios::app);
  69.    
  70.     if (output.fail())
  71.     {
  72.         std::cout << "Error opening output file " << output_name << "." << std::endl;
  73.     }
  74. }
  75.  
  76. void close_file_input(std::ifstream &close)
  77. {
  78.     close.close();
  79. }
  80.  
  81. void close_file_output(std::ofstream &close)
  82. {
  83.     close.close();
  84. }
  85.  
  86. void sort_input (std::ifstream &input_file1, std::ifstream &input_file2, std::ofstream &output_file)
  87. {
  88.     //storage for input ints coming from two files
  89.     int num1;
  90.     int num2;
  91.  
  92.     while((!input_file1.eof()) && (!input_file2.eof()))
  93.     {
  94.         std::cout << "inside the while loop" << std::endl;
  95.  
  96.         while(!input_file1.eof())
  97.         {
  98.             input_file1 >> num1;
  99.             std::cout << num1 << std::endl;
  100.         }
  101.  
  102.         while(!input_file2.eof())
  103.         {
  104.             input_file2 >> num2;
  105.             std::cout << num2 << std::endl;
  106.         }
  107.  
  108.         do
  109.         {  
  110.  
  111.             if(num1 > num2)
  112.             {
  113.                 output_file << num2 << " " << std::endl;
  114.                 input_file2 >> num2;
  115.                 goto next;
  116.             }
  117.  
  118.             if(num2 > num1)
  119.             {
  120.                 output_file << num1 << " " << std::endl;
  121.                 input_file1 >> num1;
  122.                 goto next;
  123.             }  
  124.  
  125.             if(num1 == num2)
  126.             {
  127.                 output_file << num1 << " " << num2 << " " << std::endl;
  128.                 input_file1 >> num1;
  129.                 input_file2 >> num2;
  130.             }
  131.  
  132.             next:;
  133.  
  134.         }while ((!input_file1.eof()) || (!input_file2.eof()));     
  135.     }  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement