Guest User

Untitled

a guest
Feb 4th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1.     #include <fstream>
  2.     #include <vector>
  3.     #include <iostream>
  4.  
  5.     //Forward declarations of functions.
  6.  
  7.     std::vector<double> readFile                (); //Reads the file into a vector, and then returns it
  8.  
  9.     void                outputToTable           (const std::vector<double> temperatures); //Produces a new text file in the form of a table
  10.  
  11.     double              celciusToFahrenheit     (const double celcius); //Converts Celsius to Fahrenheit
  12.  
  13.     double              celciusToKelvin         (const double celcius); //Converts Celsius to Kelvin
  14.  
  15.     int main()
  16.     {
  17.         std::vector<double> temperatures = readFile(); //Fill the list
  18.         outputToTable(temperatures);    //Output to table
  19.     }
  20.  
  21.     std::vector<double> readFile() //Function to read file, and return a list
  22.     {
  23.         std::ifstream file;
  24.         file.open("Celsius.txt"); //Your text file name here
  25.  
  26.         std::vector<double> temperatures; //List of temperatures
  27.  
  28.         if (file.is_open()) {
  29.             double val;
  30.             while (file >> val) {
  31.                 temperatures.emplace_back(val);
  32.             }
  33.         }
  34.         else {
  35.             std::cout << "File could not be opened" << std::endl;
  36.         }
  37.         return std::move(temperatures);  //More efficient to use std::move (not sure if I am doing it right though,
  38.                                         //You are better off returning without the std::move) aka "return temperatures
  39.     }
  40.  
  41.     void outputToTable(const std::vector<double> temperatures)
  42.     {
  43.         std::ofstream file;
  44.         file.open("Temperature Table");
  45.  
  46.         file << "Celsius \t Fahrenheit  \t Kelvin \n";
  47.  
  48.         for (auto& tempreture : temperatures) //This is called a "for each loop". I suggest you look this one up!
  49.         {
  50.             file    << tempreture                       << "\t \t \t " // a \t means "print a tab" aka a few spaces.
  51.                     << celciusToFahrenheit(tempreture)  << "\t \t \t"
  52.                     << celciusToKelvin(tempreture)     << "\n";
  53.         }
  54.  
  55.  
  56.     }
  57.  
  58.     double celciusToFahrenheit(const double celcius)
  59.     {
  60.         return celcius * 1.8 + 32; //I just googled the formula to convert this
  61.     }
  62.  
  63.     double celciusToKelvin(const double celcius)
  64.     {
  65.         return celcius + 273.15; //I just googled the formula to convert this
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment