Thread: Help with adding two numbers taken as Strings !!

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    51

    Help with adding two numbers taken as Strings !!

    Hey,

    I was trying to solve a problem that required to add one hundred 50 digit numbers. Since there is no way to hold such a huge number. I read that storing them in strings is the way to go. Unfortunately, I was caught midway while trying to do the same.

    Here's the source code;
    Code:
    #include<stdio.h>
    #include<string.h>
    const char * addTwoStrings(char *number1,char *number2)
    {
    	int i = strlen(number1);
    	printf("%s\n",number1);
    	printf("%d\n",i);
    	return "hello";
    }
    
    
    int main(void)
    {
    	char filename[] = "inputNumbers.txt";
    	FILE *FP;
    	char c;
    	int i=0,j=0;
    	char string[2][9] = {'\0'};
    	FP = fopen(filename,"r");
    	if(FP ==NULL)
    		{
    			printf("Error opening file");
    			return 0;
    		}
    	while((c= fgetc(FP)) != EOF)
    		{
    			if(c == '\n')
    				{
    					i++;
    					j=0;
    					continue;
    				}		
    			string[i][j++] = (char)c;
    		}
    	addTwoStrings(string[0],string[1]);	
    /*	for(i=0;i<2;i++)
    		{
    			for(j=0;j<9;j++)
    				{
    					printf("%d",string[i][j]-'0');
    				}
    			printf("\n");
    		}*/
    	return 0;
    }
    And the text file is this.
    Code:
    123465789
    321654987
    This isn't the exact huge number, but I wanted to try it out with lower number before trying out with the original huge ones.

    As you can see, I am trying to store the numbers in a two-dimensional array. However when I and try to pass the single number as an parameter to the AddTwoStrings() method, It actually passes the entire number as such.

    When I pass string[0],string[1] it should pass the first and second number from the files as the two numbers instead of the whole number as such.

    The function AddTwoStrings() doesn't do anything as of now, I encountered this error when I was testing the code till this part.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you effectively implemented gets: the code that you wrote to read a line from the file only checks for '\n' with no consideration as to whether there is enough space to store the entire number on that line, plus the terminating null character. It so happens that you used a 2D array, i.e., 2 arrays of 9 chars, and that your input consists of 9 digit numbers. Therefore, you ended up storing the 9 digit numbers with no space for the null character. Therefore, you have a buffer overflow. The problem that you observed is because the characters ended up contiguous in memory.

    Since you know that the numbers will be at most 50 digits, I suggest that you use a 51 character array to store them, the +1 being for the null character, then use fgets to read into each array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suggest you start with.
    Code:
    void addTwoStrings( const char *op1, const char *op2, char *result ) {
    }
    
    int main ( ) {
      char num1[] = "123456789";
      char num2[] = "11";
      char result[51] = { 0 };
      addTwoStrings( num1, num2, result );
      printf("Result=%s\n", result);
      return 0;
    }
    It will teach you about place value and carry.
    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. Adding two numbers represented as strings
    By slayer92 in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2013, 09:01 AM
  2. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  3. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 04:37 PM
  4. adding numbers
    By Allen_Buchannon in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 05:43 PM
  5. Adding two numbers
    By Maybe in forum C Programming
    Replies: 2
    Last Post: 03-11-2003, 01:55 PM