Thread: Porblem with an error() function

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Naples Italy
    Posts
    3

    Porblem with an error() function

    Hello, I'm studying C++ by two months using the book : programming principles and practice using C++, I'm on chapter 5 now studying error reporting. The author in this chapter uses this example to show to the readers the run time errors.

    Code:
     #include "std_lib_facilities"
    
        int area(int length, int width)
        {
    
        return length*width;
    
        }
    
        int framed_area(int x, int y)
        {
    
        return area(x - 2, y - 2);
    
        }
    
        int main()
        {
    
        int x = -1;
        int y = 2;
        int z = 4;
    
        
        int area1 = area(x, y);
    
        int area2 = framed_area(1, z); 
        int area3 = framed_area(y, z); 
    
        }


    this code produces a run-time error due to the division of area1 with 0.
    Subsequently, the author introduces a facilitation contained in the header file which he wrote called error (). My problem is that when this function call should produces a system error over a string literal that we passed as an argument to the function, instead my program code at runtime produces a screen that reports:


    >> Microsoft visual C++ runtime library

    >> debug error!
    >> program:..dio
    >> 2013\Projects\learnprogramming\Debug\learnprogramm ing.exe
    >> abort() has been called

    What could be happened ? is this a problem of the function implementation ?

    I thought that there was something wrong with this program so I decided to write a simple selection program using error() but it produced the same errors.

    for example :

    Code:
    int main ()
    {
    
    int n = 0; 
    
    cout << "Enter a positive value "; 
    cin >> n; 
    
    if(n >0)
    cout << "Ok\n"; 
    else error ("not a positive value"); 
    }
    I am just wondering if the message I get is normal ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no division, let alone a division by zero, in the first sample of code you have shown.

    The second sample of code has no relationship to the first. Without knowing what error() does, nobody has a hope of guessing whether it is normal for it to call abort().



    If you are going to post, the onus on you is to provide information - including code samples - that is actually representative of your problem. You have not done that, so you have effectively eliminated the chance of any forum member providing you useful advice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Location
    Naples Italy
    Posts
    3
    Quote Originally Posted by grumpy View Post
    There is no division, let alone a division by zero, in the first sample of code you have shown.

    The second sample of code has no relationship to the first. Without knowing what error() does, nobody has a hope of guessing whether it is normal for it to call abort().



    If you are going to post, the onus on you is to provide information - including code samples - that is actually representative of your problem. You have not done that, so you have effectively eliminated the chance of any forum member providing you useful advice.
    Sorry I forgot to post the link with the descritption of error
    here is : http://www.stroustrup.com/Programmin...b_facilities.h

    For the code I have shown, there is no division because I'm a mess, sorry, I forgot to write just one line after the definition of area 3 : double ratio = double(area1) / area3;
    Last edited by pibo99; 01-20-2015 at 11:04 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The error() function provided by that sample (from Stroustrup) throws an exception. If an exception is not caught, std::terminate() is called. std::terminate(), by default, calls abort(). Your code does not catch any exceptions.

    So, yes, when calling that error() function, it is normal for abort() to be called. No, that is not a problem with the function implementation - throwing an exception is certainly one logical way of handling an error. The problem is that your code is not catching and handling exceptions - which it needs to do in order to respond to them.

    Since area3 will have a value of zero in your code, dividing by area3 will divide by zero. That is, strictly speaking, undefined behaviour. One common behaviour, however, is to terminate due to division by zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is normal because you let an exception propagate out of scope in main. You need to catch it with a try-catch block.
    I would also suggest you run the program using the debugger to catch where these exceptions are thrown if you need to debug your program.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Porblem In Coding !!!!
    By RahulDhanpat in forum C Programming
    Replies: 29
    Last Post: 02-11-2008, 10:31 PM
  2. Porblem with structures and pointers
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-23-2007, 11:40 PM
  3. Porblem with Scanf
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 04-22-2007, 11:34 PM
  4. Homework Porblem
    By jwmonroe in forum C++ Programming
    Replies: 8
    Last Post: 06-24-2006, 11:42 PM
  5. Porblem with strtok
    By pokks in forum C Programming
    Replies: 6
    Last Post: 01-04-2006, 10:14 AM

Tags for this Thread