What would be the error code for this?? Am I using perror() at right place?? The output shows "No error"

#include <stdio.h>
#include <conio.h>
#define MAX_VAL 32000

int main()
{
  start:
  int x;
  printf("Enter a value for x= ");
  scanf("%d", &x);
  if (x > MAX_VAL)
   {
     perror("");
     getch();
     system("cls");
     goto start;
   }
  printf("%d",x);
  getch();
  return 0;
}

You need to look up what perror() actually does. It's not in the wrong place, you're just not using it for the purpose it was intended.

Also, don't use conio.h and getch(). They are non-standard and will cause you headaches when you get to the other 98% of the compilers that don't use them. There are standard ways to accept input that all C++ compilers have available.

And use a loop instead of gotos. It's bad programming practice to use them when there are better control structures available.

okay! I'll have a look over the perror().

don't use conio.h and getch()

So, which function shall i use?? for c++ we can use cin.get(); but what to use for C??

The output shows "No error"

Probably because there's no error. perror() writes the value of errno in human readable form, along with an optional user provided message. It can literally be implemented like this:

/*
    @description:
        Maps the error number in errno to an error message.
*/
void perror(const char *s)
{
    if (s && *s) {
        fputs(s, stdout);
        fputs(": ", stdout);
    }

    puts(strerror(errno));
}

There are only a handful of places in the standard library that are required to set errno to something specific (mostly math functions and multibyte en/decoding); everywhere else is optional and implementation defined. Neither printf() nor scanf() are included in those handful of places.

You're not using perror() incorrectly, as Walt mentioned, but for an application defined error such as exceeding MAX_VAL, you're on your own for setting errno:

if (x > MAX_VAL) {
    errno = ERANGE;
    perror(NULL);
}

So, which function shall i use?? for c++ we can use cin.get(); but what to use for C??

The equivalent of cin.get() in C is getchar().

ohkay.. so I'll have to change the value for ERANGE manually as per my requirements??

writes the value of errno in human readable form

msdn.microsoft.com/en-us/library/s37ta3wt.aspx

Is this the list you're talking about??

Is this the list you're talking about??

For Microsoft's perror(), yes. Other implementations of the standard library may have different values of errno that are supported and different messages. For example, my implementation presently maps errno values like this:

extern struct _errno_mapping __errno_map[] = {
    {EDOM,     "EDOM",     "Domain error"},
    {EILSEQ,   "EILSEQ",   "Encoding error"},
    {ERANGE,   "ERANGE",   "Range error"},
    {EINVAL,   "EINVAL",   "Invalid argument"},
    {ENOENT,   "ENOENT",   "No such file or directory"},
    {ENOMEM,   "ENOMEM",   "Not enough memory"},
    {EIO,      "EIO",      "I/O error"},
    {ENFILE,   "ENFILE",   "Too many open files"},
    {EACCESS,  "EACCESS",  "Permission denied"},
    {EUNKNOWN, "EUNKNOWN", "Unknown error"},
    {ESETP,    "ESETP",    "Error setting file position"},
    {EGETP,    "EGETP",    "Error getting file position"},
    {ESFMT,    "ESFMT",    "Unexpected string format"},
    {-1,       "",         ""} /* Sentinel record with an invalid errno value */
};

Where the sentinel implicitly gets converted to "Unrecognized error" in strerror()'s implementation:

/*
    @description: 
        Maps the number in errnum to a message string.
*/
char *strerror(int errnum)
{
    size_t i;

    /* Simple case for no error */
    if (errnum == 0)
        return "No error";

    /* Look for a matching errno code */
    for (i = 0; __errno_map[i].code >= 0; ++i) {
        if (__errno_map[i].code == errnum)
            return (char*)__errno_map[i].msg;
    }

    return "Unrecognized error"; /* Give up ;) */
}
extern struct _errno_mapping __errno_map[] = {
{EDOM, "EDOM", "Domain error"},
{EILSEQ, "EILSEQ", "Encoding error"},
{ERANGE, "ERANGE", "Range error"},
{EINVAL, "EINVAL", "Invalid argument"},
{ENOENT, "ENOENT", "No such file or directory"},
{ENOMEM, "ENOMEM", "Not enough memory"},
{EIO, "EIO", "I/O error"},
{ENFILE, "ENFILE", "Too many open files"},
{EACCESS, "EACCESS", "Permission denied"},
{EUNKNOWN, "EUNKNOWN", "Unknown error"},
{ESETP, "ESETP", "Error setting file position"},
{EGETP, "EGETP", "Error getting file position"},
{ESFMT, "ESFMT", "Unexpected string format"},
{-1, "", ""} /* Sentinel record with an invalid errno value */
};

Awesome! So i can display any error statement like this??

Awesome! So i can display any error statement like this??

It depends on what you mean by "this". Note that what I showed you is the internal setup of perror() on my standard library implementation. You can't simply toss my __errno_map into your code and expect perror() to change how it works on the compiler you're using.

okay got it!! Thanks!!

Be a part of the DaniWeb community

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