Hi,

I am new to programming.. and am studying IT at Uni

Over the weekend, I managed to write a java class that handles errors and loops until receiving valid input for a range of different datatypes. I am looking to write something like this in C (as my other subject deals with C). I have been googling etc and still have not really come up with what I want. Now I am aware that C++ has the try/catch option like Java, but what do I have available in C? The books I looked at didnt address my issue, and I have skipped around trying to find an answer, so i thought i might try here.

What do I do to make sure that my scanf ("*i", &myInt) statements get their correct input and not some char that makes it fall through the questionnaire in my code ..and so,..?

Help??

Thanks.. in advance.
Nate

Recommended Answers

Read this article at Dr.Dobb's http://www.ddj.com/cpp/184402023

SUMMARY: Standard Library string functions like strcpy() are not considered secure by today's standards. The Managed String Library is one possible solution.

And here is a link of a very good book

Jump to Post

All 2 Replies

Now I am aware that C++ has the try/catch option like Java, but what do I have available in C?

C can simulate exception handling with the setjmp.h library, but that is kind of an advanced C thing. ;) Usually error handling in C is done by receiving error codes as the return value from functions, as output parameters to functions, or with a global error code object:

#include <stdio.h>

#if defined(_MSC_VER)
#define FLUSH(strm) ((strm)->_cnt = 0)
#else
#define FLUSH(strm) FlushInputStream(strm)
#endif

void FlushInputStream(FILE *strm)
{
    int ch;
    do ch = getc(strm); while (ch != '\n' && ch != EOF);
}

void PrintError(char const* msg, FILE *istrm)
{
    fputs(msg, stderr);
    FLUSH(istrm);
}

/* error handling with a return code */
int GetInt1(int* value)
{
    return scanf("%d", value) == 1 &&
           0 < *value && *value < 6;
}

/* error handling with an output parameter */
int GetInt2(int* error)
{
    int value = 0;

    if (!(scanf("%d", &value) == 1 &&
          0 < value && value < 6))
    {
        *error = 1;
    }

    return value;
}

static int errorCode;

/* error handling with a global error object */
int GetInt3()
{
    int value = 0;

    errorCode = !(scanf("%d", &value) == 1 &&
                  0 < value && value < 6);

    return value;
}

int main()
{
    int value;
    int error;

    /* error handling with a return code */
    if (GetInt1(&value)) printf("read %d\n", value);
    else PrintError("GetInt1 failed\n", stdin);

    /* error handling with an output parameter */
    error = 0;
    value = GetInt2(&error);
    if (error == 0) printf("read %d\n", value);
    else PrintError("GetInt2 failed\n", stdin);

    /* error handling with a global error object */
    errorCode = 0;
    value = GetInt3();
    if (errorCode == 0) printf("read %d\n", value);
    else PrintError("GetInt3 failed\n", stdin);
}

Do you already have your code set up to recognize different error cases? Stream input in C is tricky. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.