data abstraction

here is my code

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <string>
#include <vector>
using namespace std;


string type;					//declare string variable to hold string type
int age_val;					//declare int variable to hold age of obejct
float dmt_val;					//declare float variable to hold diameter of object
string name_val;				//declare string variable to hold name of object

//class type named MyObjects
class MyObjects
{
private:
	int age; float dmt; string name;			//delcare members private to this class
	class MyObjects* next;						//declare private pointer to the class
	void operator = (const MyObjects& obj) {}

public:
	MyObjects(int a, float d, string n) : age(a), dmt(d), name(n), next(0) {}	//constructor function and intiallize members
	MyObjects(const MyObjects& obj) : age(obj.age), dmt(obj.dmt), name(obj.name), next(0) {}
	~MyObjects() { if (next != 0) delete next; }			//destructor function
	string get_name() const { return name; }				//function returns name of object
	int get_age() const { return age; }						//function returns age of object	
	float get_diameter() const { return dmt; }				//function returns diameter of object
	int year_passed(int years) { return age += years; }		//function return age incremented by years
	MyObjects* get_next() const { return next; }
	void set_next(MyObjects* obj) { next = obj; }

};

vector <MyObjects*> objs;		//declare vector

//Overlaod output operator
ostream& operator << (ostream& out, const MyObjects& obj)		
{
	out << "(" << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ")";
}

//exception for incomplete object
struct inc_obj 
{
	string str;
	inc_obj(string iStr) { str = iStr; }			//constructor function

};

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

//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 and check for errors
int readObjects(string type) {


	bool age_ready = false, dmt_ready = false, name_ready = false;
		
	
						
	if (type == "string") {
		if(cin >> name_val)	{			//get value of name
			name_ready = true;
			if (dmt_ready && age_ready)
				objs.push_back(new MyObjects(age_val, dmt_val, name_val));
			else if (age_ready || dmt_ready)
				throw inc_obj(name_val);
		}
	}

	else if (type == "int") {
		if (cin >> age_val) {					//get value of age
			if (age_val > 0) {
				age_ready = true;
				if (dmt_ready && name_ready)
					objs.push_back(new MyObjects(age_val, dmt_val, name_val));
			}
			else if (age_val < 0) throw neg_int(age_val);		//if negative value is entered throw an exception
			else if (!cin) throw miss_val(age_val);			//if no integer is entered throw an exception
			
		}
	}

	else if (type == "float") {
		if (cin >> dmt_val) {					//get value of diameter
			dmt_ready = true;
			if (age_ready && name_ready)
				objs.push_back(new MyObjects(age_val, dmt_val, name_val));
		}
	}

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

	
	return 0;
		
}


//Function main call readObjects fucniton and uses try and catch method to catch errors
//then outputs a summary of the errors encountered and the objects in the format //(name,diameter,age)
int main() {	

	cin >> type;							//get first output

	while(cin)	{						//while loop keeps reading input

		try {

		if (type.empty())				
			break;						//break out of the whle loop if type is empty
			readObjects(type);				//call readObjects function 
			cin >> type;					//get input again
		}
		catch (inc_obj) {					//catch incomplete object error 
			cerr << "Error: incomplete object encountered:" << name_val << "\n";		
			cin.clear();
			cin >> type;
		}
		catch (wrong_type) {		  			//catch unrecognized type error
			cerr << "Error: unrecognized type:" << type << "\n";				
			cin.clear();
			cin >> type;
		}
		catch (neg_int) {					//catch negative integer error
			cerr << "Error: no integer should be negative:" << age_val << "\n";		
			cin.clear();
			cin >> type;
		}
		catch (miss_val) {					//catch missing value error
			cin.clear();
			cin >> type;
			cerr << "Error: missing value for int type:" << type << "\n";
		}
		
	}

	//output summary
	for (int i = 0; i < objs.size(); ++i) {
		cout << *(objs[i]);
	}
	cout << "\n";
	return 0;

	
}


It compiles with no errors but when i run it i dont get any ouptu, just a blank space.
Here is my input:
int 3 float 2.3 string jo string mat string adam int 5 float 6.5 string car

Any ideas what's wrong or missing?
Last edited on
¿what should happen?
In `readObjects()' it is impossible to activate more than one flag at the time.

Also, read the warnings.
any solutions to that? I needed so to look something like this.
Input:
int 49 float 7.9 string t1 string mathew string car float 3.7 int 3 string mat
My output should be :
(t1,7.9,49) (mathew,7.9,49) (car,7.9,49) (mat,3.7,3)

Also what do you mean by warnings? I get no errors when compiling
Last edited on
I said warnings, no errors. This is the important one
foo.cpp: In function ‘std::ostream& operator<<(std::ostream&, const MyObjects&)’:
foo.cpp|39 col 1| warning: no return statement in function returning non-void [-Wreturn-type]
you need to return out;


About `readObjects()'. Instead of passing just one word, you may pass the stream and let it read the whole object.
1
2
3
4
5
6
7
8
9
10
MyObjects readObjects(std::istream &input){
   //...
   while(not (age_set and dmt_set and name_set) ){
      if(not (input>>type_specifier) )
         throw input_failure();
      //resolve if age, dmt or name
   }

   return MyObjects(age, dmt, name);
}



By the way, you are leaking memory as you don't have any delete, but there is no need to use dynamic allocation in the first place. You may just std::vector<MyObjects> obj;
Last edited on
Topic archived. No new replies allowed.