taking data from a file,converting, then writing to a sperate file

Hi Everyone,
I am new to C++ and am trying to complete a program that will read integers from a .txt document, convert them in the program, and then write them in a separate .txt document. i am encountering some issues that i cannot find answers for in the forum and i am hoping someone here can point me in the right direction. My first issue is that i have no idea how to take several integers from a .txt file, run those through a formula, and then have the conversions (myc2f,myf2c) outputted to a separate, but predetermined .txt file. the second issue i am having with this assignment is that i cannot figure out how to remove an option from the menu once it has been selected. i will copy the whole assignment below so that there is at least a perspective. thank you in advance for your time.

p.s. i hope this isn't coming off as me wanting someone to do my work for me, i just need a bit of direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{

	int option, x;
	int line = x;
	double Farenheit, Celsius;
	int myc2f, myf2c;

	ifstream infile;
	ofstream outfile;
	cout << "=========================================================" << endl
		<< " Thomas Hardy" << endl
		<< " Solution for Assignment #6" << endl
		<< "=========================================================\n\n";
	do
	{
	cout<< "	1) Convert Celsius to Fahrenheit" << endl
		<< "	2) Convert Farenheit to Celsius" << endl
		<< "	3) Exit" << endl;
	cin >> option;

		if (option == 1)
		{
			infile.open("data_Celsius.txt");
			myc2f = x*1.8 + 32;
			outfile.open("Hardy_Fahrenheit_Output.txt");
			
			cout << "Celsius to Fahrenheit process is initiated" << endl
				<< "Celsius to Fahrenheit process is complete\n\n" << endl
				<< string(50, '\n'); //clear screen 50 lines
		}
		else if (option == 2)
		{
			infile.open("data_Fahrenheit.txt");
			myf2c = (x - 32)*(5 / 9);
			outfile.open("Hardy_Celsius_Output.txt");
			cout << "Fahrenheit to Celsius process is initiated" << endl
				<< "Fahrenheit to Celsius process is complete\n\n" << endl;
		}
		else if (option != 3)
		{
			cout << "Invalid entry" << endl;
		}
		else
		{
			cout << "------------------------------------" << endl;
			cout << "End of Assignment # 6" << endl;
			cout << "------------------------------------" << endl;
			system("pause");
			return 0;
		}
	} while (option != 3);

	system("pause");
	return 0;



-Build a command-line C++ application that allows user to convert pre-recorded temperature values from Fahrenheit to Celsius or from Celsius to Fahrenheit. Related data is provided along with this assignment.

Once completed, user will be prompted by three options;
• Option 1 Convert Celsius to Fahrenheit
• Option 2 Convert Fahrenheit to Celsius
• Option 3 Exit
Any other entry must lead to warning message ( i.e “ Invalid Entry”). And then, user will be asked
choose one of the listed options again. Program will not stop unless “Option 3“ is selected.

IF OPTION 1 SELECTED:
1. User will be prompted by “Celsius to Fahrenheit Process is initiated “
2. Then program reads temperatures in degrees Celsius from the given input file
(data_Celsius.txt), calls the function (myc2f) to convert the temperature to degrees
Fahrenheit, and write the converted temperatures into an output file named as
(LastName_Fahrenheit_Output.txt).
3. Then, program will produce a confirmation message on the screen “Celsius to
Fahrenheit Process is completed “
4. Then ask user to choose from remaining options (i.e. Option 2, Option 3). Get the user’s
entry, and then clear the screen.

IF OPTION 2 SELECTED:
1. User will be prompted by “Fahrenheit to Celsius Process is initiated “
2. Then program reads temperatures in degrees Fahrenheit from an input file
(data_Fahrenheit.txt), calls the function ( myf2c) to convert the temperature to degrees Celsius, and write the converted temperature into an output file named as
(LastName_Celsius_Output.txt).
3. Program will produce a confirmation message on the screen “Fahrenheit to Celsius
Process is Completed “
4. Then ask user to choose from remaining options (i.e. Option 1, Option 3). Get the user’s
entry, and then clear the screen.

IF OPTION 3 SELECTED:.
1. User will Prompted by “Exit from the Program “
2. Then, Exit from the Program.
Program will not stop unless “Option 3“ is selected.

EXPECTATIONS
You will be submitting one c++ source code document which will generate two output data files based on user’s input. You do NOT need to submit the input data files.
Minimum 3 functions must be designed.

3 functions.
1. A void function for the “MAIN MENU”.
2. myc2f (). to Convert Celsius to Fahrenheit
3. myf2c () to Convert Fahrenheit to Celsius

INPUT DATA FILES:
You will be developing code to read the input data numbers from given data files.
• Pre-recorded Celsius Data points (data_Celsius.txt)
• Pre-recorded Fahrenheit Data points (data_Fahrenheit.txt)

OUTPUT DATA FILES: Your code must produce output data files based on user preference.
IF OPTION 1 SELECTED then the output data file LastName_Fahrenheit_Output.txt
IF OPTION 2 SELECTED then the output data file LastName_Celcius_Output.txt
Do NOT submit any data file.

WARNING MESSAGE

WARNING MESSAGE 1 : There must be a warning message of “ Invalid Entry !” : if the
user enters a choice (option) which is NOT given in the menu items ( i.e. 5), user will be
prompted by “ Invalid Entry !” and the menu items must be shown again to give the user an
opportunity to make correct selection.
Last edited on
I would highly recommend you turn the conversions into functions which allow you to pass streams to them. It will significantly increase the ease with which you do this.

Also, I would highly recommend you open the files at the start. Granted, still a total noob here, but Stroustrup's book (for beginners, like me) brought up an interesting point that - should there be an error with file I/O, it's better to detect it at the start, rather than go through motions until something unexpected happens. If you think about it, it's pretty good advice, and again, will simplify your assignment greatly.

ANYHOO...

If it were me friend, I would have a little something like (and most of this is going to be pseudo-code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <fstream>
#include <iostream>
using namespace std;

ifstream& get_infile(const string& s); //Use this to "load" your input file
ofstream& get_outfile(const string& s); //Ditto

void to_celsius(istream& is, ostream& os); //Work gets done here
//You'll obviously want another for fahrenheit

int main()
{
	//Get your fahrenheit input - pretend fah_in
	//Get your celsius input - pretend cel_in - likely poor naming, but whatevs
	//Get your fahrenheit output - fah_out
	//Get your celsius output - cel_out
	
	/*Having a good time, got my menu up, lalala, OH! The user wants to convert fahrenheit to celsius!
	not a problem!*/
	
	to_celsius(fah_in, cel_out);
	
	//Or something like that.
}

ifstream& get_infile(const string& s)
{
	ifstream ifs{s};
	if(!ifs){ //Doing a little error checking
	cerr << "Couldn't locate input file " << s << endl;
	exit(1);
	}
	return ifs;
}
ofstream& get_outfile(const string& s)
{
	//Same story above, but errors are rarer because the system will create a file for you.
}

void to_celsius(istream& is, ostream& os)
{
	double temp{0};
	while(is >> temp){
		//temp = convert and stuff...
		os << temp;
	}
}


EDIT: It occurs to me that it may still be a bit confusing. Effectively, I'm suggesting you relegate most of the "hard" work to functions. Your main file should be nearly empty. Your main file above? That thing is REALLY busy, and I don't like it. What this is doing is asking you to provide a name for your files (use cin or whatever you want) to name an fstream:
1
2
3
4
5
cout << "Give me a file name for your celsius input file!";
string s;
cin >> s;
ifstream cel_in = get_infile(s);
//do that with the rest of the files, substituting input/output where appropriate 


And then, once you get to the menu selection, "What do you want to do?" stage, both selections boil down to a single function call that passes the appropriate files. You know:

1
2
3
if(/*the user wants to convert celsius to fahrenheit*/){
//pass the celsius input file and fahrenheit output file as arguments to the appropriate //function
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>      // use later
#include <string>       // use later

void display_menu_1();

int main()
{
    char option = ' ';
    display_menu_1();
    
    while ( std::cin >> option && tolower(option) != '3')
    {
        switch(option)
        {
            case '1':
                std::cout << "Case 1 is active\n";
                // code and function calls go here
                break;
                
            case '2':
                std::cout << "Case 2 is active\n";
                // code and function calls goes here
                break;
                
            default:
                std::cout << "Invalid Entry\n";
        }
        
        display_menu_1();
    }
    
    std::cout << "Exit from Program\n";
    
    return 0;
}

void display_menu_1()
{
    std::cout << "***************************\n";
    std::cout << "* 1. Celsius -> Farenheit *\n";
    std::cout << "* 2. Farenheit -> Celsius *\n";
    std::cout << "* 3. Exit program         *\n";
    std::cout << "***************************\n";
}
Last edited on
Topic archived. No new replies allowed.