Adding numbers in strings in C++
Results 1 to 5 of 5

Thread: Adding numbers in strings in C++

  1. #1
    Member
    Join Date
    Sep 2003
    Posts
    42

    Question Adding numbers in strings in C++

    Ok, I have been working on a project lately that required me to write a function that added together strings of numbers. Such as "123" + "321" = "444". It all needs to stay as a string. I wrote the function, and it works up until a certain point and I can't figure out what is wrong with it. The first while loop works perfectly, but then the second screws up and it's almost exactly the same as the first. Here is the code and the functions it itself uses. Why does it not work? or if you have a better function written, what is it?
    Code:
    #include <string.h>
    #include <iostream.h>
    
    void cstradd(char *fnum, char *snum, char *lnum);
    void cstrrev(char *str);
    
    int  main()
    {
    	char a[12] = "2242442245"; //"12345678910";
    	char b[11] = "242"; //"1112131415";
    	char c[] = "";
    	cstradd(a,b,c);
    	cout << a << " + " << b << " = " << c << endl;
    	return 0;
    }
    
    void cstradd(char fnum[], char snum[], char lnum[])
    {
    	cstrrev(fnum);
    	cstrrev(snum);
    	int count = 0;
    	char carry = 0;
    	while(fnum[count] != 0 && snum[count] != 0)
    	{
    		char temp[2] = "0";
    		temp[0] = (fnum[count] - 48) + (snum[count] - 48) + carry;
    		carry = 0;
    		if(temp[0] > 9)
    		{
    			temp[0] = temp[0] + 38;
    			carry = 1;
    		}
    		else
    		{
    			temp[0] = temp[0] + 48;
    		}
    		strcat(lnum, temp);
    		temp[0] = 0;
    		count++;
    	}
    	/* This is the while loop that fails, some how it is editing snum and just totally screws up, it's just like the first while loop but with only fnum, I don't know what is screwing up */
            while(fnum[count] != 0) 
    	{
    		char temp[2] = "0";
    		temp[0] = (fnum[count] - 48) + carry;
    		carry = 0;
    		if(temp[0] > 9)
    		{
    			temp[0] = temp[0] + 38;
    			carry = 1;
    		}
    		else
    		{
    			temp[0] = temp[0] + 48;
    		}
    		strcat(lnum, temp);
    		temp[0] = 0;
    		count++;
    	}
    	while(snum[count] != 0)
    	{
    		char temp[2] = "0";
    		temp [0] = (snum[count] - 48) + carry;
    		carry = 0;
    		if(temp[0] > 9)
    		{
    			temp[0] = temp[0] + 38;
    			carry = 1;
    		}
    		else
    		{
    			temp[0] = temp[0] + 48;
    		}
    		strcat(lnum, temp);
    		temp[0] = 0;
    		count++;
    	}
    	if(carry == 1)
    	{
    		char temp[2] = "1";
    		strcat(lnum, temp);
    	}
    	cstrrev(lnum);
    	cstrrev(fnum);
    	cstrrev(snum);
    }
    
    void cstrrev(char str[])   // This works for reversing a string
    {
    	int l=strlen(str)-1;
    	for(int x=0;x<l;x++,l--)
    	{
    		str[x]^=str[l];  //triple XOR Trick
    		str[l]^=str[x];  //for not using a temp
    		str[x]^=str[l];
    	}
    }
    --------------------------------------------------------------------
    E, the modern pi.

  2. #2
    alright... i've noticed two problems with your code... but neither one solves the bigger problem

    You need to add a count=0; statement before each while loop. Otherwise the second and third loops will probably never be used.

    The big problem comes from the strcat(str1, str2) statment. I've run some different numbers and each time snum is deleted at different times. *shrug* But i can say WITHOUT A DOUBT that the problem comes from the strcat(str1, str2) statement.

    Here's what i got from my msdn help system:
    StrCat

    Appends one string to another.

    LPTSTR StrCat(
    LPTSTR psz1,
    LPCTSTR psz2,
    );

    Parameters
    psz1
    [in/out] Address of a null-terminated string to be appended to. It must be large enough to hold both strings.
    psz2
    [in] Address of the string to be appended to psz1.
    Return Values
    Returns a pointer to psz1, which holds the combined strings.

    i'll keep working on this and when (if?) i can fix the problem i'll post my findings
    You are so bored that you are reading my signature?

  3. #3
    Junior Member
    Join Date
    Oct 2002
    Posts
    5
    Use stringstreams. They'll make the solution almost trivial.

  4. #4
    Member
    Join Date
    Sep 2003
    Posts
    42

    ok

    Ok, I looked at that one strcat in there and put some output around it and tested the values, your right. But what I don't get, is that it is doing the same way as in the first while loop. It's weird. As for string streams, I actually would rather find out why mine is failing, so I know for future reference.
    --------------------------------------------------------------------
    E, the modern pi.

  5. #5
    ok, i fixed the program... how? by removing strcat of course!

    in place of strcat in the first loop, use this statement: lnum[count]=temp[0];
    strcat is meant for much longer strings.. but this is just an add on.

    Second... you've already done the addition, so the second loop is mostly useless. i just commented that whole thing out... Then i commented the Third loop out...

    So, at this point you have the CHANGED numbers stored in lnum. All that's left is to fill in any missing values. Remember, if both fnum and snum had values, then lnum would have changed at that location.

    Finally, you need to keep the final carry==1 loop to fill in the top location (if needed)

    Here's how i have the final cstradd sub program

    void cstradd(char fnum[], char snum[], char lnum[])
    {
    cstrrev(fnum);
    cstrrev(snum);
    int count = 0;
    char carry = 0;
    while(fnum[count] != 0 && snum[count] != 0)
    {
    char temp[2] = "0";
    temp[0] = (fnum[count] - 48) + (snum[count] - 48) + carry;
    carry = 0;
    if(temp[0] > 9)
    {
    temp[0] = temp[0] + 38;
    carry = 1;
    }
    else
    {
    temp[0] = temp[0] + 48;
    }
    //cout<<snum<<"before\n";
    lnum [count] = temp[0];
    //cout<<snum<<"after\n";
    temp[0] = 0;
    count++;
    }
    //cout<<fnum<<","<<snum<<","<<lnum<<", first loop out\n";

    count = 0;
    while(fnum[count] != 0)
    {
    if (lnum[count] == 0)
    {
    //cout<<fnum[count]<<","<<lnum<<"\n";
    lnum[count]=fnum[count];
    }
    count++;
    }

    count = 0;
    while(snum[count] != 0)
    {
    if(lnum[count]==0)
    {
    lnum[count]=snum[count];
    }
    count++;
    }


    if(carry == 1)
    {
    char temp[2] = "1";
    strcat(lnum, temp);
    }
    cstrrev(lnum);
    cstrrev(fnum);
    cstrrev(snum);
    }

    Now, i've only tested this with a few numbers... you should test it with more numbers (don't forget about negatives)

    Also, you could re-write the whole thing and use the VAL(str) function... at least i think that's the right one... i've not used it in a while.

    ENJOY
    You are so bored that you are reading my signature?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •