• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Whats wrong with this c++ code?

Jassi

Diamond Member
#include <fstream.h>
#include <iostream.h>


ifstream fin("num.txt");
int number;


while (!fin.eof())
{
number = fin.get();

}

cout << number;

fin.close();

-------

All I am doing is going through a file and getting the last number, I will expand it later to read constants defined a file to compute a solution to 6 Matrix problem. I don't know much about file I/O and even C++ (as I realized this semester to my heartbreak, this code is borrowed from a tutorial site) so any help will be greatly appreciated.
 
when posting a programming problem you should post the error so we don't have to guess what's wrong

in this case i'm gonna assume the number read is not what you expect. .get() reads a character instead of getting and parsing a number for you

so instead of
number = fin.get();
use
number << fin;

im not sure if there are other errors so i guess you can try it see what happens

edit: oh yeah i assume your code after the #include statements are in the main function. if not maybe you should pick up a book/find a tutorial on c++ first

edit2: also it's best to use the non-.h version of the c++ headers, eg <iostream> and <fstream>, then putting an using statement once after them eg:

#include <fstream>
#include <iostream>
using namespace std;
 
I miss the days when my programming assignments are only a quarter page long 🙁

<-- just came off a 10-hour programming spree
 
Originally posted by: screw3d
I miss the days when my programming assignments are only a quarter page long 🙁

<-- just came off a 10-hour programming spree

lol I know what you mean.

<--- Recently came off a 24 hour programming spree (I took a 6 hour break to sleep).
 
Back
Top