Hi guys, I dont know why when my code outputs in an output file, it gives me a NAN but my operations are correct. Is it bc the code is reading the text file wrong? Or is it something wrong with the code itself. ESE224inputfile.txt
Hi guys, I dont know why when my code outputs in an output file, it gives me a NAN but my operations are correct. Is it bc the code is reading the text file wrong? Or is it something wrong with the code itself. ESE224inputfile.txt
Use the debugger to find out when does the NaN occur. From there, you would have a better idea what is causing this.
You should check for successful I/O: check the return value of getline, check that the files were opened correctly. Incidentally, you should prefer to create the file objects while initialising them, e.g.,Originally Posted by skeetskeetlit
This is the principle that variables should be declared near first use, in as small a scope as feasible. Likewise, it makes no sense to write:Code:ifstream fin("whatever.txt"); if (!fin) { // there's an error opening the file so report it and handle it }
when you could have written:Code:string line1; line1=input;
but then it looks like you don't need this line1 variable in the first place.Code:string line1 = input;
By the way, it would be better to post your code in [code][/code].
Last edited by laserlight; 09-21-2018 at 04:46 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
On the face of it, you never actually read a line (or line1) from your input file, but blindly assume that your conversion of the cin line (on line 31) is successful.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.