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 6113527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:52:10+00:00 2026-05-23T14:52:10+00:00

I have a function that should be passed a string with two numbers (as

  • 0

I have a function that should be passed a string with two numbers (as a regex: /-?[0-9]+ -?[0-9]+/) and return the second.

I’ve decided that the program should do error checking. First, it should test if the string is actually of the desired form; second, it should ensure that the first numbers (the ones that are not returned) are sequential.

Now I’ve been programming for a long time and this is not a difficult task. (It’s made slightly more difficult by the fact that the numbers need not fit into a machine word.) But my question is about how I should do this rather than how I can. All of the solutions I’ve come up with are somewhat ugly.

  • I could use a global variable to keep the values in and compare them (or just leave the value there if it’s NULL); this seems like The Wrong Thing.
  • I could pass one or both of the return value and the last/current line’s first number by reference and modify them
  • I could use the return value to give a bool for there was/was not an error
  • etc.

So any thoughts relating to the proper way to deal with error-checking of this sort in C would be welcome.


This is related to a much more theoretical question I asked on cstheory. For reference, here is the function:

char*
scanInput(char* line)
{
    int start = 0;
    while (line[start] == ' ' || line[start] == '\t')
        start++;
    if (line[start] == '#')
        return NULL;    // Comment
    if (line[start] == '-')
        start++;
    while (line[start] >= '0' && line[start] <= '9')
        start++;
    while (line[start] == ' ' || line[start] == '\t')
        start++;
    int end = start;
    if (line[end] == '-')
        end++;
    while (line[end] >= '0' && line[end] <= '9')
        end++;
    if (start == end)
        return NULL;    // Blank line, or no numbers found
    line[end] = '\0';

    return line + start;
}

and it is called like so:

while(fgets(line, MAX_LINELEN, f) != NULL) {
    if (strlen(line) > MAX_LINELEN - 5)
        throw_error(talker, "Maximum line length exceeded; file probably not valid");
    char* kept = scanInput(line);
    if (kept == NULL)
        continue;
    BIGNUM value = strtobignum(kept);
    if (++i > MAX_VECLEN) {
        warning("only %d terms used; file has unread terms", MAX_VECLEN);
        break;
    }
    // values are used here
}
  • 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-23T14:52:10+00:00Added an answer on May 23, 2026 at 2:52 pm

    Ultimately, you are going to need to isolate and convert both big numbers in each line. To check that the first number on the line is the one that follows the previous, you will have to keep a record of the last such number found. So, you will probably need a structure such as:

    BIGNUM old_value = 0;  // See notes below
    
    while (fgets(line, sizeof(line), f) != 0)
    {
        BIGNUM value1;
        BIGNUM value2;
        if (ScanDoubleBigNum(line, &value1, &value2) != 0)
            ...handle line format error...
        if (old_value == 0 || are_consecutive(old_value, value1))
        {
            // OK - valid information found
            // Release old_value
            old_value = value1;
            process(value2);
            // Release value2
        }
        else
            ...handle non-consecutive error...
    }
    

    The are_consecutive() function determines whether its second argument is one greater than its first. The process() function does whatever you need to do with the second value. The ScanDoubleBigNum() function is related to your ScanInput() but it reads two values. The actual code will call another function (call it ScanBigNum()) containing about half of ScanInput() (since that contains essentially the same code twice), plus the conversion that currently occurs in your loop. The code in ScanDoubleBigNum() will call ScanBigNum() twice. Note that ScanBigNum() will need to identify where the scan finishes so that the second call can continue where the first stopped.

    I’m taking the liberty of assuming that a BIGNUM is an allocated structure identified by a pointer, so the initialization BIGNUM old_value = 0; is a way of indicating there is no value yet. There is presumably a function to release a BIGNUM. If this is incorrect, then you need to adapt the proposed code to accommodate the actual behaviour of the BIGNUM type. (Is this based on OpenSSL or SSLeay code?)

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

Sidebar

Related Questions

I have an IF statement that consists of two separate function calls passing values
I have a function that is supposed to take a string, append things to
I have to write a library which contains a function that takes two strings
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that gets x(a value) and xs(a list) and removes all
I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that I use called sqlf(), it emulates prepared statements. For
I have a function that is effectively a replacement for print, and I want
I have a function that takes a struct, and I'm trying to store its

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.