trouble with command line argument processing and fstream

This is a discussion on trouble with command line argument processing and fstream within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program for a postfix notation calculator and I take in filenames on the commandline and then process ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    trouble with command line argument processing and fstream

    I'm writing a program for a postfix notation calculator and I take in filenames on the commandline and then process them from there. However I can only ever process the first command. Can someone please help me out. the push, pop, top, implementations are standard for a stack. the code is here: http://www.dexdog.com/postcalc.txt , I appreciate any help you guys can give me. If you need more info let me know. Thanks again.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    My guess is that since you are reusing the same ifstream variable for each file, you need to call clear() when you close the previous file to clear the failbit and eofbit. Otherwise when you start to read the second file it thinks it has reached eof already.

    Also, you might want to change how you read in values. I don't know if it will matter in this particular case, but you generally want to check the return value of the read:
    Code:
     if (in >> dub)
    This is better than checking for eof() where you do it because it handles invalid input and because eof is not set until after you attempt to read past the end of the file, which might mean that you use a value one extra time.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    Daved, thank you very much, that was indeed the case. I overlooked that time and time again. However now I have a new problem. I have inplemented some checks to make sure all the values in the file are valid, and if not have tried to reset the stack and also ignore the input until a specified character is implemented (the ignore is done very ugly I must say). I have updated the original file in the link. the Changes are marked with //NEW. Thank you very much for your help.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    Ok, I figured that part out, it needed to be
    Code:
    if (in.peek() != '+' && in.peek() != '-' && in.peek() != '*' && in.peek() != '/' && in.peek() != '=' && in.peek() != '~' && in.peek() != '#' && in.peek() != ' ')
    instead of
    Code:
    if (in.peek() != '+' || '-' || '*' || '/' || '=' || '~' || '#' || ' ')

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21