CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2009
    Posts
    76

    Setting type restrictions/Error handling

    So I am finishing the chapter on errors in my book and it's taking me back to a program I wrote for another exercise and telling me to modify the program to write out an error if the result cannot be represented as an int.

    Currently the code is this:
    Code:
    // Celsius to Fahrenheit and Fahrenheit to Celsius
    #include "std_lib_facilities.h"
    
    double ctof (double orig_c)
    {
           double f = ((orig_c*9.0)/5.0)+32.0;
           return f;
    }
    
    double ftoc (double orig_f)
    {
           double c = ((orig_f-32.0)/9.0)*5.0;
           return c;
    }
    
    int main()
    {
        double orig_c = 0;
        double orig_f = 0;
        cout << "Input Celsius, and then fahrenheit.\n";
        cin >> orig_c >> orig_f;
        double f = ctof(orig_c);
        double c = ftoc(orig_f);
        cout << orig_c << " Celsius equals " << f << " Fahrenheit.\n";
        cout << orig_f << " Fahrenheit equals " << c << " Celsius.\n";
        keep_window_open();
    }
    Obviously the first step I need to take is to set "f" and "c" as type int, but after that when dealing with errors, I'm not sure how to put into code what I want to happen. I mean I want to say something along the lines of

    Code:
    // Celsius to Fahrenheit and Fahrenheit to Celsius
    #include "std_lib_facilities.h"
    
    class Bad_type{}        // type specifically for reporting errors from ctof and ftoc
    
    double ctof (double orig_c)
    {
           int f = ((orig_c*9)/5)+32;   // took out the .0's (they were there in first place because
                                                     // without them for some reason the answer was construed
           if (f ! int) throw Bad_type();    // obviously the if (f ! int) won't work but that's the question
           else
           return f;
    }
    
    double ftoc (double orig_f)
    {
           int c = ((orig_f-32)/9)*5;             // same comments as above
           if (c ! int) throw Bad_type();
           else
           return c;
    }
    
    int main()
    try{
        double orig_c = 0;
        double orig_f = 0;
        cout << "Input Celsius, and then fahrenheit.\n";
        cin >> orig_c >> orig_f;
        double f = ctof(orig_c);
        double c = ftoc(orig_f);
        cout << orig_c << " Celsius equals " << f << " Fahrenheit.\n";
        cout << orig_f << " Fahrenheit equals " << c << " Celsius.\n";
        keep_window_open();
    }
    catch (Bad_type){     // catches Bad_type and reports error
        cout << "Result cannot be represented as an integer."
    }
    Any ideas how I can specify the type restriction? Thanks in advance. And sorry if the code is messy I actually edited the second set of code in this post window.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Setting type restrictions/Error handling

    You could maybe try comparing the double result to double(INT_MAX)

    Code:
    double f = ((orig_c*9.0)/5.0)+32.0;
                                 
    if (f > double(INT_MAX)) throw Bad_type();
    else
        return int(f);
    Alternatively, you could equate the result to a type that is guaranteed to be larger than an int and compare with INT_MAX
    Code:
    _int64 f = ((orig_c*9.0)/5.0)+32.0;
                                 
    if (f > INT_MAX) throw Bad_type();
    else
        return int(f);
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured