Stop the creation of an Object

What I’m trying to do is if the denominator is zero, I want to return an error message and delete the object. I’ve written the error message, now I need it to delete the object, or stop its creation.

Background about the program:
Rational is the name of the class. The below code is a constructor that accepts a string.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 Rational::Rational(std::string inputString){
	std::cout << "Inside the constructor that accepts 1 string" << std::endl;
	std::stringstream ss;
	
	ss << inputString;
	ss >> numerator;
	ss.ignore();
	ss >> denominator;
	equation = inputString;
	simplifyInt();

	if(denominator == 0){
		std::cout << "Error. Zero on the bottom of a fraction, therefore it is undefined!" << std::endl;
		std::cout << "The object was not created, because the fraction is invalid." << std::endl;
		//need to add a line of code here to delete the object or stop it's creation
	}
	else{
	std::cout << numerator << "/" << denominator << std::endl;
	std::cout << equation << std::endl;
	}
}


This is what I'm looking for:
User Input: Rational A("25/0");

Result: Error. Zero on the bottom of a fraction, therefore it is undefined!
The object was not created, because the fraction is invalid.


Thanks for taking a look.
There really are only 2 options:

1) Throw an exception. This is really the only way to stop creation of an object.

or

2) Put the object in a known "bad" state (like set a boolean or something) and check for that bad state in all your member functions to ensure the user isn't trying to use an incomplete/bad object.
Thank you for the reply!

Could you go into a little more detail in what you mean by, "Throw an Exception."?

Of the two mentioned this one sounds more appealing.

The other way it sounds like I would have change my class to include one more private member that is a bool. I would then set that bool value to false in the constructor if it wasn't a valid input. Then inside my main function I would test for this false bool value when ever I did something with my object. Is that correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>

int gcd(int a, int b)
{
    a = std::abs(a);
    b = std::abs(b);

    while (a)
    {
        int temp = a;
        a = b % a;
        b = temp;
    }

    return b;
}

struct Rational
{
    Rational(std::string);
    friend std::ostream& operator<<(std::ostream&, const Rational&);

private:
    void reduce();

    std::string equation;
    int numerator;
    int denominator;
};

void Rational::reduce()
{
    int divisor = gcd(numerator, denominator);
    numerator /= divisor;
    denominator /= divisor;
}

Rational::Rational(std::string inputString) : equation(inputString)
{
    std::istringstream in(inputString);

    char dummy;
    if (!(in >> numerator) || !(in >> dummy) || !(in >> denominator))
        throw std::invalid_argument("ERROR: Invalid argument - \"" + inputString + '"');

    if (denominator == 0)
        throw std::domain_error("ERROR: Denominator cannot be 0 - \"" + inputString + '"');

    reduce();
}

std::ostream& operator<<(std::ostream& os, const Rational& r)
{
    os << r.numerator;

    if (r.denominator != 1)
        os << " / " << r.denominator;

    return os;
}

int main()
{
    std::string line;
    while (std::getline(std::cin, line) && line.size())
    {
        try {
            std::cout << Rational(line) << '\n';
        }

        catch (std::exception& ex)
        {
            std::cout << ex.what() << '\n';
        }

        std::cout << '\n';
    }
}


http://ideone.com/B7Nm0X
Last edited on
Thank you very much! I was able to successfully implement an exception.
Topic archived. No new replies allowed.