Adding an exception to this program

I need function Storesale and Findsale to check the index validity. If the index is invalid, StoreSale and FindSale should generate an exception instead of exiting the program immediately. Then the main program should handle the exception by displaying an error message. I'm not quite sure how to do this. I think catch and throw statements would work but I don't exactly get how to do them properly. Any help with this would be appreciated. Thanks again.

salesrecord.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SalesRecord
{
private:
	float *sales_amount;
	int number_of_sales;

public:
	SalesRecord(int howmanysales);
	SalesRecord( const SalesRecord& othersales );
	~SalesRecord();
	SalesRecord& operator= ( const SalesRecord& othersales ) ;
	void StoreSale(float newsales, int index);
	float FindSale(int index);

};


Main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include "salesrecord.h"
using namespace std;
	int main( )
	{
		SalesRecord   s1(4), s2(3);
		s1.StoreSale(2.4f, 0); s1.StoreSale(8.7f, 1);
		s1.StoreSale(6.3f, 2); s1.StoreSale(3.5f, 3);
		s2=s1;
		s1.StoreSale(3.5f, 0); 
		cout << s2.FindSale(0)<<endl;
		cout << s2.FindSale(1)<<endl;
		cout << s2.FindSale(2)<<endl;
		cout << s2.FindSale(3)<<endl;
}


salesrecord.cpp

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
#include<iostream>
#include "salesrecord.h"
using namespace std;

SalesRecord::SalesRecord(int howmanysales)
{
	int i;
	if (howmanysales<1) 
	{
		cerr << "invalid size: ";
		exit(1);
	}
	sales_amount=new float[howmanysales];
	int number_of_sales=howmanysales;
	
	for (i=0; i<number_of_sales; i++)
	{
		sales_amount[i]=0;
	}
}
SalesRecord::SalesRecord( const SalesRecord& othersales )
{
    number_of_sales = othersales.number_of_sales ;

   
    sales_amount = new float[number_of_sales] ;
    for( int i=0 ; i<number_of_sales ; ++i )
         sales_amount[i] = othersales.sales_amount[i] ;
}


SalesRecord::~SalesRecord()
{
	delete[] sales_amount;
}


void SalesRecord::StoreSale(float newsales, int index)
{
	sales_amount[index]=newsales;
}

float SalesRecord::FindSale(int index)
{
	return sales_amount[index];
}
SalesRecord& SalesRecord::operator= ( const SalesRecord& othersales )
{
   
        delete [] sales_amount ;

		number_of_sales=othersales.number_of_sales;

        sales_amount = new float[number_of_sales] ;
        for( int i=0 ; i<number_of_sales ; ++i )
             sales_amount[i] = othersales.sales_amount[i] ;
 
    return *this;
}
Certainly, you could check if the index is valid, and if not, throw some exception. Or you could use vectors and, in addition to making the program a lot simpler, use vector's own exception-throwing member functions:

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
#include <vector>
#include <iostream>
#include <stdexcept>
class SalesRecord
{
 private:
    std::vector<float> sales_amount;
 public:
    SalesRecord(int howmanysales)
        : sales_amount(howmanysales >= 0 ?
                           howmanysales :
                           throw std::logic_error("invalid size")
                       )
    {
    }
    void StoreSale(float newsales, int index)
    {
        sales_amount.at(index) = newsales;
    }
    float FindSale(int index) const
    {
         return sales_amount.at(index);
    }
};

int main()
{
    SalesRecord s(10);
    try {
        std::cout << "Setting valid sales: ";
        s.StoreSale(0.01, 1);
        std::cout << "...success\n";
        std::cout << "Setting invalid sales: ";
        s.StoreSale(0.02, 100);
        std::cout << "...success?\n";
    } catch (const std::exception& e)
    {
        std::cout << "...caught " << e.what() << '\n';
    }
}


(incidentally, your constructor has an error: it never sets the "number_of_sales" member variable, It creates a local variable under the same name instead)
Last edited on
Topic archived. No new replies allowed.