i was just playing around with this and for some reason i can't get to work...
i have a text file (num.txt) which has ONLY 4 characters they are the following: 234'EOF'
what i'm trying to do is to read the 3 characters in untill the (end of file character) thus in result i want to add their values, however all that is being added is their ASCII representations.
I know i can use the insertion operators and input (int) integers, thus i wouldn't have a problem but i want to do it using fin.get() function...
here's the simple code:
Code:#include<fstream> #include<iostream> #include<cstdlib> using namespace std; int main() { int sum = 0; char num; ifstream fin("num.txt"); if(fin.fail()) { cout << "can't open the file for input\n"; exit(1); } else { fin.get(num); while(!fin.eof()) { cout << "ASCI : " << (int)num << endl << endl; cout << "values : " << num << " " << endl << endl; sum = sum + num; fin.get(num); } } cout << "The sum of 2 3 4 is: " << sum << endl; return 0; }
Thanks in advance....
ps. i tried using atoi() but i just can't get it to work for some reason... it requires a (char constant) but i can't make this constant....?????



LinkBack URL
About LinkBacks


