can not get this to work

Pages: 12
#include <stdio.h>
#include <math.h>

int main() {


float temp_c, temp_f, result;
int i;
FILE *outputFilePtr;
outputFilePtr = fopen("output.txt", "w"); //

fprintf(outputFilePtr, "Temperature(C) Speed\n");


for (i = 0; i<100; i++)
{
temp_f = (1.8 * i) + 32; // convert from C to F


result = (5 * temp_f) + 2297;
result = result / 2457;
result = 325.8*sqrt(result);

fprintf(outputFilePtr, "%3d %8.4f\n", i, result);
}

getchar();
getchar();

return 0;
}
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
#include <stdio.h>
#include <math.h>
#include <fstream>

int main() {


	float temp_c, temp_f, result;
	int i;
	std::ofstream file;
	file.open("output.txt");
	
	file << "Temperature(C) Speed\n"; 
	
	for (i = 0; i<100; i++)
	{
		temp_f = (1.8 * i) + 32; // convert from C to F


		result = (5 * temp_f) + 2297;
		result = result / 2457;
		result = 325.8*sqrt(result);

		file << i << " " << result << "\n";		
	}

	file.close();

	getchar();
	getchar();

	return 0;
}
Last edited on
thanks for that but nothing appears on the console with this c code
std::cout << "Of course not, you are not printing to the console.\n";
sorry i'm not very good with c

C++ i'm ok

so i don't understand the above
if not how would i
1
2
fprintf(outputFilePtr, "%3d %8.4f\n", i, result);  /*prints to a file*/
printf("%3d %8.4f\n", i, result); /*print to the console*/



Also
$ ./prograrm.bin | tee file
Last edited on
#include <stdio.h>
#include <math.h>
#include <fstream>

int main() {


float temp_c, temp_f, result;
int i;
std::ofstream file;
file.open("new.txt");
FILE *outputFileptr;


file << "Temperature(C) Speed\n";

for (i = 0; i<100; i++)
{
temp_f = (1.8 * i) + 32; // convert from C to F


result = (5 * temp_f) + 2297;
result = result / 2457;
result = 325.8*sqrt(result);
fprintf(outputFileptr ,"%3d %8.4f\n", i, result); /*prints to a file*/
printf("%3d %8.4f\n", i, result);

file << ("%3d %8.4f\n", i, result);
}

file.close();

getchar();
getchar();

return 0;
}
i have this and still no luck

am novice at this

sorry
i keep getting a error with outputfileptr in line 27
Line 25: Where do you initialize (open) outputFileptr?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
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

#include <stdio.h>
#include <math.h>
#include <fstream>

int main() {


	float temp_c, temp_f, result;
	int i;
	std::ofstream file;
	FILE *outputFileptr ;
	fprintf(outputFilePtr, "%3d %8.4f\n", i, result);

outputFilePtr = fopen("new.txt", "w");


	file << "Temperature(C) Speed\n";

	for (i = 0; i<100; i++)
	{
		temp_f = (1.8 * i) + 32; // convert from C to F


		result = (5 * temp_f) + 2297;
		result = result / 2457;
		result = 325.8*sqrt(result);
		
	
		printf("%3d %8.4f\n", i, result); 


	}

	file.close();
	 
	getchar();
	getchar();
open the folder where you executable is located you should see a file called new with the populated data.

if you are using visual studio add to the preprocessor _CRT_SECURE_NO_WARNINGS

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
// ConsoleApplication6.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <stdlib.h>  /* For exit() function */
#include <math.h>


int main() 
{

	float temp_c, temp_f, result;
	int i;
	
	FILE *outputFileptr ;
	outputFileptr = fopen("new.txt","w");
                
        printf("%s","Temperature(C) Speed\n");

	fprintf(outputFileptr,"%s", "Temperature(C) Speed\n");

	for (i = 0; i<100; i++)
	{
		temp_f = (1.8 * i) + 32; // convert from C to F


		result = (5 * temp_f) + 2297;
		result = result / 2457;
		result = 325.8*sqrt(result);
		
	
                printf("%3d %8.4f\n", i, result); /*print to the console*/
		fprintf(outputFileptr, "%3d %8.4f\n", i, result);


	}

	fclose(outputFileptr);
	 
	getchar();
	getchar();

	return 0;
}
Last edited on
i have copied this code and used _CRT_SECURE_NO_WARNINGS

but the console is blank now

if i don't use _CRT_SECURE_NO_WARNINGS

i get my results but no print
of course it is meant to be blank. because your are not telling it to print to the console.
I have updated the code so that it prints to console also.
thanks for that

i'm sorry am new to this

so now outputFileptr = fopen("new.txt", "w");

it doesn't open or print to the file
sorry for all the questions
what error messages are you getting?
none it runs but no text file is been open
it does open it and write to it you just don't see it.

if you want a text editor to open it that is a different issue.

http://www.cplusplus.com/forum/beginner/6033/
Pages: 12