assigning and casting (char) to (int)

This is a discussion on assigning and casting (char) to (int) within the C++ Programming forums, part of the General Programming Boards category; i was just playing around with this and for some reason i can't get to work... i have a text ...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    assigning and casting (char) to (int)

    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....?????

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Subtract 48 from the char value to get the equiv integral. You can test the char first by making sure it is >= 48 and <=57. Just look at or print the ASCII chart.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    pretty nifty.....i haven't even thought about that....

    thanx

  4. #4
    NewBoy
    Guest
    I think Im going to go buy
    C++ for Dummies or whatever beacuse I herd that it was good for begginers

    Is it good?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type casting void *
    By Alexpo in forum C Programming
    Replies: 5
    Last Post: 06-23-2008, 03:05 AM
  2. simple question about pointers and casting
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2008, 02:25 PM
  3. tell me about a 'char'
    By Brain Cell in forum C Programming
    Replies: 4
    Last Post: 02-21-2005, 08:05 AM

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