C++ ofstream not outputting variables

Discussion in 'Mac Programming' started by upperlacon, Nov 7, 2009.

  1. upperlacon macrumors newbie

    Nov 7, 2009
    Hi, I signed up because I was having no luck finding an answer with this simply with google searches.

    My code is in C++ it compiles, runs and outputs fine in Visual Studio 2008 on a PC. Ive put it in xcode, it compiles and runs fine, but does not seem to want to output any of my double variables to the text files. To test anything would output, I simply got it to output some text to the same file. Worked fine, just seems not to want to output variables, frustrating as it worked fine on VS2008.

    Is there something I'm missing here?

    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std;
    int main()
    	cout << "Started" << endl;
    	//euler method
    	// declaring variables
    	double y0 = 0.1, y1 = 0, g = 0, h = 0.1, y00, y11, t = 0, en;
    	en = (1 - cos(y0) ) + ( ( y1*y1 ) / 2);
    	ofstream euler("euler.txt");
    	ofstream write("write.txt");
    	write << y0 << endl;
    	euler << t << '\t' << y0 << '\t' << y1 << '\t' << en <<  endl;
    	for(int i = 0; i < 1000; i++){
    		//storing nth
    		y00 = y0;
    		y11 = y1;
    		//calculating n+1th
    		t += h;
    		y0 += ( h * y11 );
    		y1 -= ( ( h * y00 ) + ( h * g * y11 ) );
    		en = (1 - cos(y0) ) + ( ( y1*y1 ) / 2);
    		euler << t << '\t' << y0 << '\t' << y1 << '\t' << en << endl;
    	cout << "Euler Method Complete" << endl;
  2. Ti_Poussin macrumors regular


    May 6, 2005
    I try it on my computer since I couldn't find the problem visually.

    There's no problem it work, make sure you have the permission to create those file inside the folder where you run your program. You can verify the file openned correctly:
    std::cout << "File cannot be created" << std::endl;

    Here's a few thing, you have a int main() that return nothing = BAD, return 0 at the end please.

    using namespace std;

    You should not use it this way, it's a bad pratice:
    std::cout << ...
    std::cin >> ...

    You don't close the file at the end
  3. Ti_Poussin macrumors regular


    May 6, 2005
    If the file is created and the double aren't output and it's the only problem, I would like to know how you compiled your code. I simply did a
    g++ -Wall -pendantic-errors code.cc -o test

    -Wall : warn on most warnings which are mostly bad code
    -pendantic-errors : turn warning into error, cause it's often the case!

    They should teach to use those flag at school, code that doesn't pass this often leak and/or have problem. It's rare particular case that may trigger a false warning, but believe me, that it's extremely rare.

    I don't know what problem you encountered, cause your code is working. Ok, some beginner errors in it but still working, try to move it to /tmp folder and test it there to ensure permission are ok.
  4. ncl macrumors member

    Aug 16, 2008
    If you're using Xcode 3, your problem may be caused by gcc 4.2 and debug mode. Here is a way to fix it:
    Note that none of these things are necessary. However, the return 0 is probably considered a good practice.
  5. upperlacon thread starter macrumors newbie

    Nov 7, 2009

    i followed your advice and that has sorted my problem.

    I don't really get taught much of the reasons of coding in a particular way, because I study physics and they put less of an emphasis on that. However I would rather try not to fall into bad habits so out of interest why is it better to use std:: instead of using namespace std at the beginning? Also why do you need to close the file?

    Thank you both for your help!
  6. ScoobyMcDoo macrumors 65816

    Nov 26, 2007
    Austin, TX
    In a program this size, the "using" statement is fine. In larger projects, it's better to fully qualify namespaces in each line of code because you may be using multiple libraries which may have duplicate symbols. The reason for namespaces is to deal with the problems of having duplicate symbols in different libraries - the "using" statement defeats the purpose.

    Just good practice, however when using the stl library as you are, it is unnecessary as the file object will close the file for you when it goes out of scope, which in your case is when the program ends.

    One other piece of advice I would give you is don't use the variable name "write" since "write" is a function name in the standard c library.
  7. Sander macrumors 6502

    Apr 24, 2008
    We're entering the realm of personal preference here, but I'm inclined to say that a "using namespace std;" on top of your .cpp file is just fine. A problem arose when people thought "I'm typing that at the top of all my .cpp files, I might as well put it in the header file." And that, of course, defeats the purpose of namespaces.

    My view: Namespaces are there to solve a problem (name clashes). If you're not having the problem, you don't need to be bothered with them.

Share This Page