Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <iostream>
- //Forward declarations of functions.
- std::vector<double> readFile (); //Reads the file into a vector, and then returns it
- void outputToTable (const std::vector<double> temperatures); //Produces a new text file in the form of a table
- double celciusToFahrenheit (const double celcius); //Converts Celsius to Fahrenheit
- double celciusToKelvin (const double celcius); //Converts Celsius to Kelvin
- int main()
- {
- std::vector<double> temperatures = readFile(); //Fill the list
- outputToTable(temperatures); //Output to table
- }
- std::vector<double> readFile() //Function to read file, and return a list
- {
- std::ifstream file;
- file.open("Celsius.txt"); //Your text file name here
- std::vector<double> temperatures; //List of temperatures
- if (file.is_open()) {
- double val;
- while (file >> val) {
- temperatures.emplace_back(val);
- }
- }
- else {
- std::cout << "File could not be opened" << std::endl;
- }
- return std::move(temperatures); //More efficient to use std::move (not sure if I am doing it right though,
- //You are better off returning without the std::move) aka "return temperatures
- }
- void outputToTable(const std::vector<double> temperatures)
- {
- std::ofstream file;
- file.open("Temperature Table");
- file << "Celsius \t Fahrenheit \t Kelvin \n";
- for (auto& tempreture : temperatures) //This is called a "for each loop". I suggest you look this one up!
- {
- file << tempreture << "\t \t \t " // a \t means "print a tab" aka a few spaces.
- << celciusToFahrenheit(tempreture) << "\t \t \t"
- << celciusToKelvin(tempreture) << "\n";
- }
- }
- double celciusToFahrenheit(const double celcius)
- {
- return celcius * 1.8 + 32; //I just googled the formula to convert this
- }
- double celciusToKelvin(const double celcius)
- {
- return celcius + 273.15; //I just googled the formula to convert this
- }
Advertisement
Add Comment
Please, Sign In to add comment