Try and catch method for errors

Here is my code so far
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
#include <iostream>
#include <string>
using namespace std;

string type;		//declare string variable to hold string type
int num;								//declare int variable to hold integer value
float flt;								//declare float variable to hold float value
string str;								//declare string varibale to hold string value

//exception for missing value for int type
struct miss_val
{
	int num;
	miss_val(int mVal) { num = mVal; }
};

//exception for negative integer values
struct neg_int
{
	int num;
	neg_int(int negVal) { num = negVal; }			//constructor fucntion 
};

//exception for unrecognized type
struct wrong_type
{
	string type;
	wrong_type(string wType) { type = wType; }		//constructor function
};

//function to read inputs summarize and output results
int readObjects(string type) {
		
	int sCount = 0;							//declare and initialize int variable to hold number of string items
	int iCount = 0;							//declare and initialize int variable to hold number of integer items
	int fCount = 0;							//declare and initialize int variable to hold number of float items
						
	if (type == "string") {
			cin >> str;		//get value of string
			sCount++;								//increment sCount for each string data in input
		}

	if (type == "int") {
			cin >> num;	//get value of int
			iCount++;								//increment iCount for each int data in input
			if (num < 0) throw neg_int(num);  //if negative value is entered throw an exception
			if (!cin) throw miss_val(num);	  //if no integer is entered throw an exception
			
		}

	if (type == "float") {
			cin >> flt;	//get value of float fCount++;				//increment fCount for each float data in input
		}

	else 
		throw wrong_type(type);				//if uncrecognized type is entered throw exception	

	//output summary
	cout << "int " << iCount << "; string " << sCount << "; float " << fCount << ";" << endl;
	return 0;
		
}


//Function main reads data from the standard input 
//then outputs a summary of how many data items of each type are in the input
int main() {	

	cin >> type; 

	while(cin)

		try {

			readObjects(type);
			cin >> type;
		}
		catch (wrong_type) {
			cerr << "Error: unrecognized type:" << type << "\n";
			cin.clear();
			cin >> type;
		}
		catch (neg_int) {
			cerr << "Error: no integer should be negative:" << num << "\n";
			cin.clear();
			cin >> type;
		}
		catch (miss_val) {
			cerr << "Error: missing value for int type:" << type << "\n";
			cin.clear();
			cin >> type;
		}

	}


Here is my input:
int 5 string hello float 3.9 int -12
output:
Error: unrecognized type: int
Error: unrecognized type: string
int 0; string0 0; float 1:
Error: no integer should be negative: -12

I don't know why it only increments fCount and catches wrong type for int and string when it shouldn't
Last edited on
The count variables are non-static local variables so they are reset each time the function is called.

You have an if statement where you check if the type is "float" but in the else-part you always throw. That means if the type is "int" or "string" it will throw. You probably want to chain the if statements with else if so that the else-part only runs if all the other if conditions are false.

1
2
3
4
5
6
7
8
9
if (type == "string") {
	...
} else if (type == "int") {
	...
} else if (type == "float") {
	...
} else {
	...
}
A couple of problems here:

1) In readObjects, after testing if type == "int", you fall through to the next test for float. Since an int is not a float, you execute the else and throw wrong_type.

When you complete processing for a type, you should return. There is no point in continuing on to subsequent tests.

2) Your counters in readObjects are local variables. They go out of scope when readObjects exists. You're not going to accumulate total counts since readObjects gets only one object.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

I fixed the problems :
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
#include <iostream>
#include <string>
using namespace std;

string type;							//declare string variable to hold string type
int num;								//declare int variable to hold integer value
float flt;								//declare float variable to hold float value
string str;								//declare string varibale to hold string value
int sCount = 0;							//declare and initialize int variable to hold number of string items
int iCount = 0;							//declare and initialize int variable to hold number of integer items	
int fCount = 0;							//declare and initialize int variable to hold number of float items

//exception for missing value for int type
struct miss_val
{
	int num;
	miss_val(int mVal) { num = mVal; }
};

//exception for negative integer values
struct neg_int
{
	int num;
	neg_int(int negVal) { num = negVal; }			//constructor fucntion 
};

//exception for unrecognized type
struct wrong_type
{
	string type;
	wrong_type(string wType) { type = wType; }		//constructor function
};

//function to read inputs summarize and output results
int readObjects(string type) {
						
					
	if (type == "string") {
			cin >> str;								//get value of string
			sCount++;								//increment sCount for each string data in input
		}

	else if (type == "int") {
			cin >> num;	
			if (num > 0) iCount++;							//get value of int//increment iCount for each int data in input
			if (num < 0) throw neg_int(num);		//if negative value is entered throw an exception
			if (!cin) throw miss_val(num);			//if no integer is entered throw an exception
			
		}

	else if (type == "float") {
			cin >> flt;								//get value of float
			fCount++;								//increment fCount for each float data in input
		}

	else 
		throw wrong_type(type);				//if uncrecognized type is entered throw exception	

	//output summary
	cout << "int " << iCount << "; string " << sCount << "; float " << fCount << ";" << endl;
	return 0;
	
}
		



//Function main reads data from the standard input 
//then outputs a summary of how many data items of each type are in the input
int main() {

	cin >> type;
	while (cin){
		
		try {

			readObjects(type);
			cin >> type;
		}
		catch (wrong_type) {
			cerr << "Error: unrecognized type:" << type << "\n";
			cin.clear();
			cin >> type;
		}
		catch (neg_int) {
			cerr << "Error: no integer should be negative:" << num << "\n";
			cin.clear();
			cin >> type;
		}
		catch (miss_val) {
			cerr << "Error: missing value for int type:" << type << "\n";
			cin.clear();
			cin >> type;
		}
		
	}
		return 0;

	}

	



I still have some issues though; Here is my input:
int 3 int -2 string hello int float 3.2 adc float 2.2
Output:
int 1; string 0; float 0;
Error: no integer should be negative: -2
int 1; string 1; float 0;
Error: missing value for type int: int
int 1; string 1; float 1;
Error: unrecognized type: adc
int 1; string 1; float 2;

I would like my output to look like that :
Error: no integer should be negative: -2
Error: missing value for type int: int
Error: unrecognized type: adc
int 1; string 1; float 2;

I tried putting the cout in main fucntion outside while loop but the the output wouldnt display, it just output my errors and keeps reading
I tried putting the cout in main function outside while loop but the the output wouldn't display, it just output my errors and keeps reading

Since you call readObjects once per loop, and your cout is inside readObjects, you're going to display the counters for every iteration of the loop.

The cout should be after line 96. However, for that to work, the test of cin at line 73 must return false. Or you can add the following at line 76:
76
77
  if (type.empty())
    break;

That will allow you to break out of the while loop simply by entering an empty type.


Topic archived. No new replies allowed.