Thread: My output from an output file is giving NAN but my operations are correct

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    1

    My output from an output file is giving NAN but my operations are correct

    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.txtMy output from an output file is giving NAN but my operations are correct-screen-shot-2018-09-21-4-48-25-pm-jpgMy output from an output file is giving NAN but my operations are correct-screen-shot-2018-09-21-4-49-59-pm-jpg

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use the debugger to find out when does the NaN occur. From there, you would have a better idea what is causing this.

    Quote Originally Posted by skeetskeetlit
    Is it bc the code is reading the text file wrong?
    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.,
    Code:
    ifstream fin("whatever.txt");
    if (!fin) {
        // there's an error opening the file so report it and handle it
    }
    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:
    string line1;
    line1=input;
    when you could have written:
    Code:
    string line1 = input;
    but then it looks like you don't need this line1 variable in the first place.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libcurl not giving output
    By madhu_d in forum C Programming
    Replies: 1
    Last Post: 04-02-2014, 02:07 PM
  2. Simple lines_to_svg file not producing correct output
    By canuxforryan in forum C Programming
    Replies: 3
    Last Post: 01-30-2014, 12:09 AM
  3. always giving output as 0.000000
    By royroy in forum C Programming
    Replies: 2
    Last Post: 11-15-2012, 08:55 AM
  4. Why is this giving me this output?
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 03-16-2012, 12:51 PM
  5. output for array of pointers is not giving correct values
    By nkrao123@gmail. in forum C Programming
    Replies: 8
    Last Post: 09-03-2011, 07:40 AM

Tags for this Thread