Hi there,
I'm getting on pretty well with a project for uni I've got to do, although I'm currently slumped at a bit. I'm running a loop to constantly read numbers into a set of equations and I'm required to output these to an excel file. I've sussed making the Excel file as I require but I'm unsure as how to print out the variables into the file itself. Heres the snippet of code I'm using -

if ( v > 4.47 && v < 25  )

	ofstream outFile  ("H:\\workspace\\Windfarms2.csv");
	outFile << "Wind Farm Data, Wind Speed, Power Generated" << endl;
	outFile << s1, ss, p << endl;
	if (!outFile)
	cout << "Error opening file" << endl;
			return -1;

	else if (v <4.47 || v==4.47)
	{ cout << "The cut-in speed has not been reached, therefore power cannot be generated/n";}
	{cout << "The Windspeed is too high, therefore power cannot be generated.\n";}

	return 0;

		cout << "Error opening file" << endl;

In the code, s1, ss and p are the variables which are required to be printed out in the file. At this current stage, I know line 6 is wrong, but thats the part I'm stuck with.
Can anyone help atall?

And I know I've been warned on these forums about the errors which may be involved when using .eof, but my lecturer said I was just to use that :$!

Edited by YeMiller: n/a

8 Years
Discussion Span
Last Post by YeMiller

You just need to add the commas outFile << s1 << ',' << ss << ',' << p << endl One problem you might encounter is if the text filed contains a comma. In that case you have to enclose it in double quotes.

For example: 1997,Ford,E350,\"Super, luxurious truck\" The backslash \ goes in your c++ program, not in the file itself.

Here is more about those csv files.

Edited by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.