Thread: Converting character array to integer array

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    Converting character array to integer array

    It seems like my old thread had died because the people helping me are now offline and no one else is repsponding so I am just gonna start this new post.

    This is an assigment I recieved for one of my classes:

    Write a program that reads two positive integers that are 20 or fewer digits in length and then ouputs their sum. Your program will read the digits as values of type char so that the number 1234 is read in as the four characters '1', '2', '3', '4'. After they are read into the program that characters are changed to values of type int. The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elments in the array ater the data is read. Perform the addition by implementing hte usual pencil-and-paper algorithm. The result of the addition is stored in an array the same size as the input arrays and the result is written to the screen. If the result of the addition is an iteger with more than the maximum number of digits, the program should print a message saying it has encountered "integer overflow".

    So far this is what I have written:

    Code:
    include <iostream>
    using namespace std;
    
    
    const int Size = 20;
    int main ()
    {
    
    	char FirstNumber [Size];
    	char SecondNumber [Size];
    	int Length;
    	int Length2;
    	int Num1 [Size];
    	int Num2 [Size];
    
    	cout<<"Enter the First Number : ";
    	cin>>FirstNumber;
    
    	cout<<"Enter the Second Number : ";
    	cin>>SecondNumber;
    
    	Length = strlen (FirstNumber);
    
    	Length2 = strlen (SecondNumber);
    
    	Num1 [Length] = FirstNumber [Size] - '0';
    	Num2 [Length2] = SecondNumber [Size] - '0';
    
    
    	cout<<Num1 [Length - 1];
    
    
    
    
    	return 0;
    }
    At this point I am attempting to convert the characters to integers. When I cout<Num1 [Length - 1] I get some trash number and not the actual number. Am I converting things correctly?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    I don't see why you have been told to read the numbers in as strings (char arrays). You should just read them in as integers.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    I know but thats how he wanted us to do it

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    The only way I know how to do this is with atoi. Though from what I gather from your posts, you're probably hemmed into doing it the way you have above, which in that case you'll have to wait for other help.

    Code:
    int main ()
    {
    
    	char FirstNumber [Size];
    	char SecondNumber [Size];
    
    	     int Num1;
    	     int Num2;
    
    	cout<<"Enter the First Number : ";
    	cin>>FirstNumber;
    
    	     cout<<"Enter the Second Number : ";
    	     cin>>SecondNumber;
    
    
    	        Num1  = atoi(FirstNumber);
    	        Num2  = atoi(SecondNumber);
    
    	cout<<Num1<<std::endl;
            cout<<Num2<<std::endl;
    
        cin.get();
    
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It seems like my old thread had died because the people helping me are now offline and no one else is repsponding so I am just gonna start this new post.

    This is a bad idea. I think it is against the rules, and regardless it leaves people with no idea what previous advice has been given. You end up getting confusing and conflicting advice from all over the place. If I understand your situation correctly, neither of the posts in this thread will help you, but they might have been able to help better if they had seen your other 3 threads on this subject.

    http://cboard.cprogramming.com/showthread.php?t=88118
    http://cboard.cprogramming.com/showthread.php?t=88074
    http://cboard.cprogramming.com/showthread.php?t=88114

    BTW, I am in the middle of replying to the first thread in that list, so look there for some further hints.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Agreed - stick to the old thread where all the useful stuff is - this is closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  2. Converting an array of byte to an array int
    By chrisfarrugia in forum C Programming
    Replies: 7
    Last Post: 03-05-2008, 08:30 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. String To Integer Array!!!!
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-29-2001, 10:15 AM