Whats wrong with this c++ code?

Jassi

Diamond Member
Sep 8, 2004
3,296
0
0
#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.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
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;
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
I miss the days when my programming assignments are only a quarter page long :(

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

EpsiIon

Platinum Member
Nov 26, 2000
2,351
1
0
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).