input and output files

Hello,

I have a bit of a problem. I need to create a .txt file which will hold a column of 11 different temperatures. My program needs to take the data from the file calculate the temperatures from celsius to Fahreheit, and then input the data into a new .txt file.

so .txtfile Celsius (11 numbers in a column)--> program calculates the temperature to Fahreheit --> program outputs the new temperatures in a column in a new .txt file.

Here's what I have so far:



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
62
63
64
65
int myc2f(int temp)
{
		int c;
	
	c = temp *1.8 +32;

	return (c);
}




int main()
{
	

	

	int  c;
	int input;
	int temp;
	
	cout << "==================================" << endl;
	cout << " \tSolution for Assignment 6  "<< endl;
	cout << "==================================" << endl;
	
	MyMenu();
	cin >> input;
	system("CLS");
	


	while (input != 3)
	{
		if (input == 1)
		{
		
			cout << " Celsius to Fahrenheit Process is initiated  "<< endl;	
			
			ifstream repos;																
                                                                        //open the file
			repos.open("data_Celsius.txt");

			while (! repos.eof())				// first column of data from file stored in temp


			{
				
				repos >> temp;
			
				c = myc2f(temp);										
                                                                     //calculated celsius to ferenheit
				

				repos.close();

				ofstream repos;														
                                                                       // write data into Celsius output file. 
				repos.open("KOHNKE_Celsius_Output.txt");
				
				repos << temp;
			
				
				if (repos.fail())
				break;


}
Last edited on
Hello Dominick55,

As I look over your code I do not see any header files, so I do not now what you have used.

I see four opening {s, but the only closing } I see looks like it is the closing } of main. Hard to tell because it is outside the code block.

Do not use "! repos.eof()". This will not work the way you think. Generally it will process 2 of the last input before the while loop will end. What is most often done is while(repos >> temp) and remove line 49 as it is not needed. This while loop has no closing }.

That is a start. I will have more in a little while.

Hope that helps,

Andy
Hi Andy,

Here's the rest of my code. Sorry to confuse you.


#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdio>
#include<cstdlib>>

using namespace std;




void MyMenu()
{

cout << "\n\n==============================================" << endl << endl;
cout << "Please eneter a number from 1-3 to pick an option" << endl << endl;
cout << "Option 1) Convert Celsius to Fahrenheit" << endl;
cout << "Option 2) Convert Fahrenheit to Celsius" << endl;
cout << "Option 3) Exit" << endl;

}




int myf2c(int temp)
{
int c;

c = ((temp - 32)) /1.8;
return (c);
}
int myc2f(int temp)
{
int c;

c = temp *1.8 +32;

return (c);
}





int main()
{




int c;
int input;
int temp;





cout << "==============================================" << endl;
cout << "DOMINICK KOHNKE" << endl;
cout << "SOLUTION FOR ASSIGNMENT #6 "<< endl;
cout << "==============================================" << endl;

MyMenu();
cin >> input;
system("CLS");



while (input != 3)
{
if (input == 1)
{

cout << "Celsius to Fahrenheit Process is initiated "<< endl;

ifstream repos; //open the file
repos.open("data_Celsius.txt");

while (!repos.eof() ) // first column of data from file stored in temp


{


c = myc2f(temp); //calculated celsius to ferenhe

repos.close();

ofstream repos; // write data into Celsius output file. (In degree Farenheit)
repos.open("KOHNKE_Celsius_Output.txt");
repos << c << endl;

repos << 19 << endl; // These are the values I need to get from the equation
repos << 32 << endl;
repos << 35 << endl;
repos << 50 << endl;
repos << 62 << endl;
repos << 68 << endl;
repos << 75 << endl;
repos << 86 << endl;
repos << 96 << endl;
repos << 104 << endl;

if (repos.fail())
break;

}






cout << "Celsius to Fahrenheit Process is completed " << endl << endl;

repos.close();

MyMenu();
cin >> input;
system("CLS");

}





else if (input == 2)
{

cout << "Fahrenheit to Celsius Process is initiated " << endl;

ifstream repos;
repos.open("data_Fahrenheit.txt");


while (repos >> temp)

{


c = myf2c(temp);

repos.close();

ofstream repos;
repos.open("KOHNKE_Fahrenheit_Output.txt");
repos << c << endl;
repos << -6 << endl;
repos << 0 << endl;
repos << 4 << endl;
repos << 9 << endl;
repos << 14 << endl;
repos << 19 << endl;
repos << 26 << endl;
repos << 31 << endl;
repos << 32 << endl;
repos << 35 << endl;




}


repos.close();

cout << "The Fahrenheit to Celsius Process is completed " << endl << endl;

MyMenu();
cin >> input;
system("CLS");
}



else
{
cout << "Invalid Entry !" << endl;

MyMenu();
cin >> input;
system("CLS");
}


}

cout << "Exit from the Program " << endl << endl;






cout << "==============================================" << endl;
cout << "END OF ASSIGNMENT 6 " << endl;
cout << "==============================================" << endl;




system("pause");
return 0;
}
Last edited on
#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdio>
#include<cstdlib>>

using namespace std;




void MyMenu()
{

cout << "\n\n==============================================" << endl << endl;
cout << "Please eneter a number from 1-3 to pick an option" << endl << endl;
cout << "Option 1) Convert Celsius to Fahrenheit" << endl;
cout << "Option 2) Convert Fahrenheit to Celsius" << endl;
cout << "Option 3) Exit" << endl;

}




int myf2c(int temp)
{
int c;

c = ((temp - 32)) /1.8;
return (c);
}
int myc2f(int temp)
{
int c;

c = temp *1.8 +32;

return (c);
}





int main()
{




int c;
int input;
int temp;





cout << "==============================================" << endl;
cout << "DOMINICK KOHNKE" << endl;
cout << "SOLUTION FOR ASSIGNMENT #6 "<< endl;
cout << "==============================================" << endl;

MyMenu();
cin >> input;
system("CLS");



while (input != 3)
{
if (input == 1)
{

cout << "Celsius to Fahrenheit Process is initiated "<< endl;

ifstream repos; //open the file
repos.open("data_Celsius.txt");

while (!repos.eof() ) // first column of data from file stored in temp


{
repos >> temp;


c = myc2f(temp); //calculated celsius to ferenhe

repos.close();

ofstream repos; // write data into Celsius output file. (In degree Farenheit)
repos.open("KOHNKE_Celsius_Output.txt");
repos << c << endl;

repos << 19 << endl;
repos << 32 << endl;
repos << 35 << endl;
repos << 50 << endl;
repos << 62 << endl;
repos << 68 << endl;
repos << 75 << endl;
repos << 86 << endl;
repos << 96 << endl;
repos << 104 << endl;

if (repos.fail())
break;

}






cout << "Celsius to Fahrenheit Process is completed " << endl << endl;

repos.close();

MyMenu();
cin >> input;
system("CLS");

}





else if (input == 2)
{

cout << "Fahrenheit to Celsius Process is initiated " << endl;

ifstream repos;
repos.open("data_Fahrenheit.txt");


while (repos >> temp)

{


c = myf2c(temp);

repos.close();

ofstream repos;
repos.open("KOHNKE_Fahrenheit_Output.txt");
repos << c << endl;
repos << -6 << endl;
repos << 0 << endl;
repos << 4 << endl;
repos << 9 << endl;
repos << 14 << endl;
repos << 19 << endl;
repos << 26 << endl;
repos << 31 << endl;
repos << 32 << endl;
repos << 35 << endl;




}


repos.close();

cout << "The Fahrenheit to Celsius Process is completed " << endl << endl;

MyMenu();
cin >> input;
system("CLS");
}



else
{
cout << "Invalid Entry !" << endl;

MyMenu();
cin >> input;
system("CLS");
}


}

cout << "Exit from the Program " << endl << endl;






cout << "==============================================" << endl;
cout << "END OF ASSIGNMENT 6 " << endl;
cout << "==============================================" << endl;




system("pause");
return 0;
}
Hi Andy,

Sorry for confusing you.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdio>
#include<cstdlib>>

using namespace std;




void MyMenu()
{
	
	cout << "\n\n==============================================" << endl << endl;
	cout << "Please eneter a number from 1-3 to pick an option" << endl << endl;
	cout << "Option 1) Convert Celsius to Fahrenheit" << endl;
	cout << "Option 2) Convert Fahrenheit to Celsius" << endl;
	cout << "Option 3) Exit" << endl;
	
}




int myf2c(int temp)
{
	int c;

	c = ((temp - 32)) /1.8;
	return (c);
}
int myc2f(int temp)
{
		int c;
	
	c = temp *1.8 +32;

	return (c);
}





int main()
{
	

	
	
	int c;
	int input;
	int temp;
	

	

	
	cout << "==============================================" << endl;
	cout << "" << endl;
	cout << "SOLUTION FOR ASSIGNMENT #6  "<< endl;
	cout << "==============================================" << endl;
	
	MyMenu();
	cin >> input;
	system("CLS");
	


	while (input != 3)
	{
		if (input == 1)
		{
		
			cout << "Celsius to Fahrenheit Process is initiated  "<< endl;	
			
			ifstream repos;																//open the file
			repos.open("data_Celsius.txt");

			while (!repos.eof() )				// first column of data from file stored in temp


			{
				repos >> temp;
				
				
				c = myc2f(temp);										//calculated celsius to ferenhe

				repos.close();

				ofstream repos;														// write data into Celsius output file. (In degree Farenheit)
				repos.open("KOHNKE_Celsius_Output.txt");
				repos << c << endl;
				
				repos << 19  << endl;
				repos << 32  << endl;
				repos << 35  << endl;
				repos << 50  << endl;
				repos << 62  << endl;
				repos << 68  << endl;
				repos << 75  << endl;
				repos << 86  << endl;
				repos << 96  << endl;
				repos << 104 << endl;
				
				if (repos.fail())
				break;

			}

				
			

			

			cout << "Celsius to Fahrenheit Process is completed " << endl << endl;
		
			repos.close();

			MyMenu();
			cin >> input;
			system("CLS");
		
		}

	
		
		
		
		else if (input == 2)
		{
			
			cout << "Fahrenheit to Celsius Process is initiated  " << endl;
		
			ifstream repos;
			repos.open("data_Fahrenheit.txt");
			
			
			while (repos >> temp)
		
			{
			
				
				c = myf2c(temp);
				
				repos.close();

				ofstream repos;
				repos.open("KOHNKE_Fahrenheit_Output.txt");
				repos << c   << endl;
				repos << -6  << endl;
				repos <<  0  << endl;
				repos <<  4  << endl;
				repos <<  9  << endl;
				repos <<  14 << endl;
				repos <<  19 << endl;
				repos <<  26 << endl;
				repos <<  31 << endl;
				repos <<  32 << endl;
				repos <<  35 << endl;
				
				
				
			
			}
			
			
			repos.close();
			
			cout << "The  Fahrenheit to Celsius  Process is completed " << endl << endl;
			
			MyMenu();
			cin >> input;
			system("CLS");
		}

		
		
		else
		{
			cout << "Invalid Entry !" << endl;
			
			MyMenu();
			cin >> input;
			system("CLS");
		}


	}

	cout << "Exit from the Program " << endl << endl;






	cout << "==============================================" << endl;
	cout << "END OF ASSIGNMENT 6  " << endl;
	cout << "==============================================" << endl;




	system("pause");
	return 0;
}[code]
Last edited on
Hello Dominick55,

Sorry I had to leave my computer for awhile. Sorry for the first short message. I will look at your last post and see what I can come up with.

Right off it looks better, but I still see some problems.

In the second while loop you use "repos" as your input file and then close it just to reopen it as your output file. But when you come back to the top of the while loop at line 83 you are trying to read a closed file. Just point out a fact that you might not have considered.

Give me a little time to go over your program. And if you would a sample of the input file would help to know you ara woring with.

Andy
Hello Dominick55,

To start with:
1
2
3
4
5
6
7
8
9
#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdio>  // <--- Unless you are using C input/output this is not needed.
#include<cstdlib>  // <--- Had an exrta > at the end.

using namespace std;  // <--- Bad idea/ You should learn not to use this. 


In the function "myf2c" the calculation to the right of the = is done as a double or at least a float, but the result is stored in an int loosing the decimial portion of the double unless that is what you want.

The same applies to "myctof".

Small warnings, but something to think about.

Line 68: I would put this line in the "MyMenu" function and also validate the input to make sure that a valid number is input. Easier to do this in the "MyMenu"function and return a good number back to main than to tell the user in main and not have a good recovery. Looks like what you have does work.

Lines 80 and 81, 94 and 95, 138 and 139 and 151 and 152 it would be better to open these files outside of the while loop and to do a check to make sure they are open. Also better to have names like "inFile" and "Outfile" to keep them straight. Then close these files just before the program ends. The other problem is each time through the while loops when you open the input file you will only read the first temp in the file missing all the rest.

Each time you open the file for output you are printing the "c" that you converted followed by 10 hard coded outputs. Not what you want. It should contiue to read the input file for the next temp and output the conversion. It is not.

That takes cre of "input == 1". The same can be applied to "input == 2".

Now I have to create an input file and work on adjusting the program.

Hope that helps,

Andy
the input file looks like this for the celsius:
-9
-7
0
2
10
17
20
24
30
36
40
Thanks for the reply so far. Appreciate that.
The problem I have is I don't know how to apply the function to all the numbers in a column and then paste the calculated values into the new file.
Hello Dominick55,

Thank you for the input file. That helps.

As an idea you could write the input as:
- 9 - 7 0 2 10 17 20 24 30 36 40

If you think about it adding a first number it could be a month number. Then you could expnt the rest to cover up to 31 days of temmps. And each line would be a different month. In the end the code would not be that hard to change. Just a thought.

Having a temp on each line works, but could become hart to follow if you add more temps.

The Idea I have would go like this:
if (input == 1)
cout ...
Open input and output files
while (inFileC >> temp)
{
    convert temp
    write to outFileF
}
The rest of the if statement
before the close of the if statement close the files.


I created two input files one for Celsius and one for Fahrenheit. Then used "inFileC" for Celsius temperatures and "inFileF" for Fahrenheit temperatures. And two output files "outFileC" and "outFileF". And adjusted the File names to match what type of temp they deal with.

This way the input file can be read until there is nothing left when the input stream will fail thus causing the while loop to fail.

Work on that and let me know what you come up with.

Hope that helps,

Andy
when i run my program it only coverts the first number and stops there. Any idea why that happens?
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
while (input != 3)
{
if (input == 1)
{

cout << "Celsius to Fahrenheit Process is initiated "<< endl;	

ifstream repos;	//open the file
repos.open("data_Celsius.txt");

while (!repos.eof() )	// first column of data from file stored in temp


{
repos >> temp;


c = myc2f(temp);	//calculated celsius to ferenhe



ofstream repos;	
repos.open("KOHNKE_Celsius_Output.txt");
repos << c << endl;


if (repos.fail())
break;

}
when i run my program it only coverts the first number and stops there

Look more closely. It converts all of the numbers, but the output file is re-created on each iteration of the loop, so only the last number is retained.

The first step is to give the input and output streams different variable names. Using repos for both is bound to cause confusion, even if it worked - which it doesn't. Next, get the file open outside the loop. Do it at the start.


By the way, don't use eof() in a loop condition. That can be the cause of problems too. Instead put the input operation itself in the loop condition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    cout << "Celsius to Fahrenheit Process is initiated "<< endl;

    ifstream fin("data_Celsius.txt");            // open Input file
    ofstream fout("KOHNKE_Celsius_Output.txt");  // open Output file

    double temp = 0;

    while ( fin >> temp )              // while a value can be read from the input
    {
        fout << myc2f(temp) << '\n';   // write the converted value to output
    }

}
Thanks for the tip. It has been quite difficult to tell if I'm working with the input or out file.

Also my result comes out in a row instead of a column. How do I make the output show in a column?

Thanks for the help so far!
Dom
I figured it out. Just needed to end the line.

Thank you so much everyone!
Topic archived. No new replies allowed.