Advice on me Code

Please critique and give advice, I trying to get better at programming. Thank you.
This code is about dogs in pound awaiting to be rescued or deathrowed.

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
#include <iostream>
#include <string>
using namespace std;

struct Dogs {
	string name;
	string type;
	unsigned id;
	bool deathrow;
};


//dogs in a pound awaiting to be rescued.
void display(const Dogs & prisoner);
void display(const Dogs prisoner[], unsigned els);
void input(Dogs & prisoner);
unsigned rescued(Dogs prisoner[], unsigned els);

int main() {
	Dogs x;
	Dogs y = { "Niko", "lab", 1234, false };
	x = y;
	x.deathrow = !y.deathrow;
	display(x);

	cout << "Please input Name of dog, type, and ID: ";
	input(y);
	display(y);

	Dogs block[3] = { { "Rufus", "Lab", 123, true },{ "Toby", "Poodle", 342, false },{ "Bull", "Pitbull", 753, true } };
	display(block, 3);
	cout << "Pardoned " << rescued(block, 3) << " death sentences" << endl;
	display(block, 3);

}

void display(const Dogs & prisoner)
{
	cout << prisoner.name << " " << prisoner.type  << " " << prisoner.id
		<< (prisoner.deathrow ? "Put to sleep" : " Rescued by owner")
		<< endl;
}
void display(const Dogs prisoner[], unsigned els)
{
	for (unsigned i = 0; i < els; i++)
		display(prisoner[i]);
}
void input(Dogs & prisoner)
{
	cout << "Please input name: ";
	cin >> prisoner.name;
	cout << "Please input type: ";
	cin >> prisoner.type;
	cout << "Please input id: ";
	cin>> prisoner.id;

}
unsigned rescued(Dogs prisoner[], unsigned els)
{
	unsigned changes = 0;
	for (unsigned i = 0; i < els; i++)
	if (prisoner[i].deathrow) 
	{
		prisoner[i].deathrow = false;
		changes++;
	}
return changes;

}




Then I dynamic Memory Allocation, Is this right?

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

#include <algorithm>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

string * copy(const string a[], unsigned els);
void show(const string a[], unsigned els);
bool die(const string & msg);
void grow(string * & a, unsigned oldEls, unsigned newEls);

int main() {
	string a[] = { "Manny", "Moe", "Jack" };
	string * b = copy(a, 3);
	a[0] = "MANNY";

	show(a, 3);
	show(b, 3);
	grow(b, 3, 5);
	b[3] = "Russell";
	b[4] = "Terrier";
	show(b, 5);

	string * ptr = NULL;
	grow(ptr, 0, 4);
	ptr[0] = "Doc", ptr[1] = "Sleepy", ptr[2] = "Dopey",
		ptr[3] = "Happy";

	grow(ptr, 4, 5);
	ptr[4] = "Grumpy";

	show(ptr, 5);
	grow(ptr, 5, 2);
	show(ptr, 2);

	delete[] b;
	cout << "bye" << endl;
}

string * copy(const string a[], unsigned els) {
	string * ret = NULL;
	try {
		ret = new string[els];
	}
	catch (const bad_alloc &) {
		die("allocation failure");
	}
	for (unsigned i = 0; i < els; i++)
		ret[i] = a[i];
	return ret;
}

void show(const string a[], unsigned els) {
	for (unsigned i = 0; i < els; i++)
		cout << "[" << i << "]: \"" << a[i] << "\"" << endl;
}

void grow(string * & a, unsigned oldEls, unsigned newEls) {
	if (a == NULL && oldEls > 0)  die("grow: inconsisent size");
	string * newA = NULL;
	try {
		newA = new string[newEls];
	}
	catch (const bad_alloc &) {
		die("allocation failure");
	}
	for (unsigned i = 0, lim = min(oldEls, newEls); i < lim; i++)
		newA[i] = a[i];
	delete[] a;
	a = newA;
}


bool die(const string & msg) {
	cout << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);
}



Last one critique please

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include <iostream>
#include <string>
using namespace std;

class SafeUnsigned{
public:
    // def ctor
    SafeUnsigned( unsigned nVal = 0 );

    // accessor and mutator
    unsigned getN() const;
    void setN( unsigned nVal );

    // complain and die if answer is > UINT_MAX
    SafeUnsigned operator*( const SafeUnsigned & rhs ) const;
    // c&d if rhs is zero
    SafeUnsigned operator/( const SafeUnsigned & rhs ) const;
    // c&d if rhs is zero
    SafeUnsigned operator%( const SafeUnsigned & rhs ) const;
    // c&d if answer is > UINT_MAX
    SafeUnsigned operator+( const SafeUnsigned & rhs ) const;
    // c&d if rhs > lhs
    SafeUnsigned operator-( const SafeUnsigned & rhs ) const;

    // relational operators: no complain and die
    bool operator< ( const SafeUnsigned & rhs ) const;
    bool operator<=( const SafeUnsigned & rhs ) const;
    bool operator> ( const SafeUnsigned & rhs ) const;
    bool operator>=( const SafeUnsigned & rhs ) const;
    bool operator==( const SafeUnsigned & rhs ) const;
    bool operator!=( const SafeUnsigned & rhs ) const;

  
    friend ostream & operator<<( ostream & out, const SafeUnsigned & n );
private:
    unsigned n;
};

unsigned multiply( unsigned a, unsigned b );
unsigned divide( unsigned a, unsigned b );
unsigned mod( unsigned a, unsigned b );
unsigned add( unsigned a, unsigned b );
unsigned subtract( unsigned a, unsigned b );

bool die( const string & msg );

int main()
{
    unsigned sum(0); // same as  unsigned sum;  with ctor's def arg
    unsigned limit(10);
    for(  unsigned i(1), inc(1);
          i <= limit;
          i = i + inc
    )
        sum = sum + i;

    unsigned sum2(sum);
    cout <<"The sum up to " <<limit <<" is " <<sum2 <<endl;

    unsigned sum(0); // same as  unsigned sum;  with ctor's def arg
    unsigned limit(1000000);
    for(  unsigned i(1), inc(1);
          i <= limit;
          i = add(i, inc)
    )
        sum = add( sum, i);

    unsigned sum2(sum);
    cout <<"The sum up to " <<limit <<" is " <<sum2 <<endl;

}

unsigned multiply( unsigned a, unsigned b ){
    unsigned ret = a*b;
    if( b != 0 && ret/b != a )  die( "* overflow" );
    return ret;
}    

unsigned divide( unsigned a, unsigned b ){
    if( b == 0 )  die( "a/0 is bogus" );
    return a/b;
}

unsigned mod( unsigned a, unsigned b ){
    if( b == 0 )  die( "a%0 is bogus" );
    return a%b;
}

unsigned add( unsigned a, unsigned b ){
    unsigned ret = a + b;
    if( ret < a )  die( "add overflow" );
    return ret;
}

unsigned subtract( unsigned a, unsigned b ){
    unsigned ret = a - b;
    if( ret > a )  die( "subtract overflow" );
    return ret;
}

bool die( const string & msg ){
    cout <<"Fatal error: " <<msg <<endl;
    exit( EXIT_FAILURE );
}
Last edited on
Topic archived. No new replies allowed.