Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:59:03+00:00 2026-05-23T13:59:03+00:00

I’m trying to create a function (in C) to solve an addition that is

  • 0

I’m trying to create a function (in C) to solve an addition that is placed in a buffer variable. So far it works for equations that result in single-digit numbers, however if the result is more than one digit, it causes problems.. For example, if I place 2+3+5+2 in the buffer, outputs 102, not 12. Can anyone point out where my errors are and how I can fix them?

Here’s my code:

char buffer[50];

void *adder(void)
{
    int bufferlen;
    int value1, value2;
    int startOffset, remainderOffset;
    int i;
    char result[10];
    while (1) 
    {
        startOffset = remainderOffset = -1;
        value1 = value2 = -1;

        bufferlen = strlen(buffer);
        for (i = 0; i < bufferlen; i++) {
            if(value1 == -1)
            {
                if(isNumeric(buffer[i]))
                {
                    value1 = string2int(&buffer[i]);
                    startOffset = i;
                }
            }
            else
            {
                if(buffer[i] == 43)
                {
                    if(isNumeric(buffer[i+1]))
                    {
                        value2 = string2int(&buffer[i+1]);
                        remainderOffset = i+1;
                    }
                }
            }

            if(value1 != -1 && value2 != -1)
            {
                int k=0;
                int j=0;
                int2string((value1 + value2),result);
                /* print out the number we've found */
                fprintf(stdout, "Re:%s\n", result);
                int resultlen = strlen(result);
                for(k=startOffset; k < bufferlen; k++)
                {
                    if(j < resultlen)
                    {
                        buffer[k] = result[j];
                        j++;
                    }
                    else
                    {
                        /* shift the remainder of the string to the left */
                        printf("A1-Buffer:%s\nk=%i\n", buffer, k);
                        if(j > 0)
                        buffer[k] = buffer[k+2];
                        else
                        buffer[k] = buffer[k+2];
                        printf("A2-Buffer:%s\n", buffer);
                        i = i - remainderOffset;
                        startOffset = remainderOffset = -1;
                        value1 = value2 = -1;   
                    }
                }
            }
        }
    break;
    }

}

Edit: Ah, sorry, forgot about the extra functions. This function is for a school assignment and the isNumeric() and String2int() are exactly the same as isdigit() and atoi() and were given to us to use in the function.

Edit 2: After the numbers are added, the result has to be added back into the buffer in the expressions place like so: (2+3) -> (5) the reason is that eventually more functions will be written to handle multiplication, division, etc. This is where my main frustration lies – putting the result in place of the expression and shifting the buffer left the proper amount.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:59:03+00:00Added an answer on May 23, 2026 at 1:59 pm
    char buffer[50];
    

    Why 50 bytes? Why not 100? You should accept a pointer as an argument instead of relying on a global. Also, buffer is not a descriptive name.

    void *adder(void)
    

    The function does not return a void *; it has no return value. You might consider

    /* return value of expression */
    int evaluate_add_expression( char const *in_string )
    

    or

    /* return status code */
    int evaluate_add_expression( char const *in_string, int *out_value ) 
    

    Next, you declare a number of variables without initializing them. Move the declarations into the loop and include initializations with the declarations, i.e. int value1 = -1, value2 = -1; Of course, those names are also not descriptive at all.

                if(buffer[i] == 43)
    

    The value '+' is a better way to represent the plus character than the magic number 43.


    If I were you, I’d avail myself of the sscanf function.

    int evaluate_add_expression( char const *in_string, int *sum ) {
        int addend;
        size_t offset, offset_delta;
    
        if ( sscanf( in_string, " %d %zn", sum, &offset ) != 1 ) return 1;
    
        while ( sscanf( in_string + offset, "+ %d %zn", &addend, &offset_delta ) == 1 ) {
            * sum += addend;
            offset += offset_delta;
        }
    
        return in_string[ offset ] != '\0'; /* check that all input was consumed */
    }
    

    This works fine and is about 15% as long.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.