Output Data to CSV File

Hi guys,

I am having trouble getting my code to output the data into a CSV file. Here is the main function. Everything runs correctly and the main function works and gives the correct output in a terminal but it doesn't put the data into the csv file as I thought it should. Any help would be greatly appreciated. Thanks for taking a look.
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
int main()
{
	int no_of_disks;
	clock_t time_before, time_after;
	float diff1, diff2;

	ofstream output_file("Data.csv");
	
	for (no_of_disks = 3; no_of_disks <= 30; no_of_disks++)
	{
		time_before = clock();
		Move_Using_Four_Pegs (no_of_disks,no_of_disks, 'A' , 'B', 'D', 'C');
		time_after = clock();
		diff1 = ((float) time_after - (float) time_before);

		time_before = clock();
		Move_Using_Three_Pegs (no_of_disks, no_of_disks, 'A' , 'C', 'B');
		time_after = clock();
		diff2 = ((float) time_after - (float) time_before);

		cout << "%age change in running time for ";
		cout << no_of_disks << " disks, when there is an extra peg = ";
		cout << ((diff1 - diff2)/diff2) * 100 << endl;

		output_file << no_of_disks << ",";
		output_file << ((diff1 - diff2)/diff2)*100 << endl;
	}
}
Last edited on
I believe I need to create an output file called "data.csv" in the directory where the program is run but I am not sure what directory that is, how to find out what directory that is, or if that is actually necessary.. Help Please!!
Sorry can't really help as I haven't done this kind of thing before. What I will say is use code tags and space out your code better, it will make it much easier to read and your more likely then for someone to take the time to help you.

put your code between these:
[code][/code]

Meerkat

EDIT: Also I'll be keeping an eye on this as I want to know how to do it as well, so if you find the answer elsewhere would you mind posting your solution here?
Last edited on
Thanks for the advice, I changed the code section so that might help. All I really need help with is the
ofstream output_file("Data.csv") line

and the last two lines because that is the part that should control the output of the data. The rest is just the main function running my actual code.
Last edited on
Topic archived. No new replies allowed.