Your code works here. With a "sample.txt" file containing 5.50, it prints "5.5", but only when run from the directory where sample.txt is located.
The example run you showed is not being run from the directory where sample.txt is located:
Code:
dhcp-155-33-114-131:[COLOR="Red"]~[/COLOR] timothywiesner$ /Users/timothywiesner/Documents/infile/build/Debug/infile ; exit;
0
logout
The red hilited
~ shows the working directory is your home directory. Unless you happen to have a "sample.txt" file there, your program won't read it.
If you want to know what your program's working directory is when it runs, look up the C function getcwd(). Use it to show what the program actually has, and compare that to what you want it to have.
As a rule, I advise the use of error checking and error reporting. Right now, you have neither. If you're getting an error you won't know it, and any error that does happen won't have its cause identified. That means one of the few ways to find out what's wrong is to use the debugger to step through the code. But you aren't doing that either.
You have to do
something concrete to debug what's happening (or not happening). Guessing what's wrong is not sufficiently concrete to be a long-term debugging strategy.