I am trying to create a c++ file which reads pre-existing data files containing
ID: 3851929 • Letter: I
Question
I am trying to create a c++ file which reads pre-existing data files containing celsius or fahrenheit values and converts those values to their opposite reading (celsius of fahrenheit) and places the converted values in a new file.
The point of this is to create and use functions which get this done.
I am a beginner to any type of coding.
I have a large number of errors and any help would be greatly appericiated.
#include <iostream>
#include <fstream>
using namespace std;
void myMenu() //first function for the menu
{
cout << "Option 1: Convert Celcius to Fahrenheit" << endl;
cout << "Option 2: Convert Fahrenheit to Celcius" << endl;
cout << "Option 3: Exit" << endl;
}
int myc2f() // second function converting the the file from celsius to fahrenheit
{
string line; // define
ifstream celcfile ("data_Celsius.txt"); // open and read the file
while (!celcfile.eof()) // read and convert the file values
{
getline(celcfile, line);
line = ((9 / 5)*line +32);
ofstream mycelc("Oja_Fahrenheit_Output.txt"); // output the converted values to a new file
cout << line << endl;
}
celcfile.close();
}
int myf2c()
{
string line2; // define
ifstream fahrfile("data_Fahrenheit.txt");
while (!fahrfile.eof()) // to read and convert the file from fahr to cels
{
getline(fahrfile, line2); //possibly here
line2 = ((line2 - 32)*(5/9));
ofstream mycelc("Oja_Celcius_Output.txt"); // create new file with the outputted conversion of celcius to fahrenheit
cout << line2 << endl;
}
fahrfile.close();
}
int main()
{
cout << "----------------------------------------------" << endl;
cout << "Solution for ASSIGNMENT #" << endl;
cout << "----------------------------------------------" << endl << endl;
int xin;
myMenu();
cin >> xin; // get the users input command
system("CLS");
while (xin != 3)
{
if (xin == 1)
{
cout << "Celcius to Fahrenheit Process is initiated" << endl;
myc2f(); // initiate the conversion with the function
cout << "Celcius to Fahrentheit Process is completed" << endl << endl;
cout << "Option 2: Convert Fahrenheit to Celcius" << endl;
cout << "Option 3: Exit" << endl;
cin >> xin;
system("CLS");
}
else if (xin == 2)
{
cout << "Fahrenheit to Celcius Process is initiated" << endl;
myf2c(); // initiate the conversion with the function
cout << "Fahrenheit to Celcius Process is completed" << endl << endl;
cout << "Option 1: Convert Celcius to Fahrenheit" << endl;
cout << "Option 3: Exit" << endl;
cin >> xin;
system("CLS");
}
else
{
cout << "Invalid Entry !" << endl;
cout << "Option 1: Convert Celcius to Fahrenheit" << endl;
cout << "Option 2: Convert Fahrenheit to Celcius" << endl;
cout << "Option 3: Exit" << endl;
cin >> xin;
system("CLS");
}
}
cout << "Exit from the program" << endl << endl;
cout << "----------------------------------------------" << endl;
cout << " End of ASSIGNMENT 6" << endl;
cout << "----------------------------------------------" << endl;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
void myMenu() //first function for the menu
{
cout << "Option 1: Convert Celcius to Fahrenheit" << endl;
cout << "Option 2: Convert Fahrenheit to Celcius" << endl;
cout << "Option 3: Exit" << endl;
}
int myc2f() // second function converting the the file from celsius to fahrenheit
{
string line; // define
ifstream celcfile ("data_Celsius.txt"); // open and read the file
while (!celcfile.eof()) // read and convert the file values
{
getline(celcfile, line);
int value = atoi(line.c_str());
value = ((9 / 5)*value +32);
ofstream mycelc("Oja_Fahrenheit_Output.txt"); // output the converted values to a new file
cout << value << endl;
}
celcfile.close();
}
int myf2c()
{
string line2; // define
ifstream fahrfile("data_Fahrenheit.txt");
while (!fahrfile.eof()) // to read and convert the file from fahr to cels
{
getline(fahrfile, line2); //possibly here
int value = atoi(line2.c_str());
value = ((value - 32)*(5/9));
ofstream mycelc("Oja_Celcius_Output.txt"); // create new file with the outputted conversion of celcius to fahrenheit
cout << value << endl;
cout<<"hello world"<<endl;
}
fahrfile.close();
}
int main()
{
cout << "----------------------------------------------" << endl;
cout << "Solution for ASSIGNMENT #" << endl;
cout << "----------------------------------------------" << endl << endl;
int xin;
myMenu();
cin >> xin; // get the users input command
system("CLS");
while (xin != 3)
{
if (xin == 1)
{
cout << "Celcius to Fahrenheit Process is initiated" << endl;
myc2f(); // initiate the conversion with the function
cout << "Celcius to Fahrentheit Process is completed" << endl << endl;
cout << "Option 2: Convert Fahrenheit to Celcius" << endl;
cout << "Option 3: Exit" << endl;
cin >> xin;
system("CLS");
}
else if (xin == 2)
{
cout << "Fahrenheit to Celcius Process is initiated" << endl;
myf2c(); // initiate the conversion with the function
cout << "Fahrenheit to Celcius Process is completed" << endl << endl;
cout << "Option 1: Convert Celcius to Fahrenheit" << endl;
cout << "Option 3: Exit" << endl;
cin >> xin;
system("CLS");
}
else
{
cout << "Invalid Entry !" << endl;
cout << "Option 1: Convert Celcius to Fahrenheit" << endl;
cout << "Option 2: Convert Fahrenheit to Celcius" << endl;
cout << "Option 3: Exit" << endl;
cin >> xin;
system("CLS");
}
}
cout << "Exit from the program" << endl << endl;
cout << "----------------------------------------------" << endl;
cout << " End of ASSIGNMENT 6" << endl;
cout << "----------------------------------------------" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.