Pointers, Dynamic Arrays and Exception Handler.

closed account (yAUfizwU)
So I have this activity for programming classI'm only allowed to place codes where it says "add code here". I managed to code some of it but when i tried to call the voids MilesPerrGallon and displaySort (pointed by comments), it always results of compile error. Can somebody pls help me


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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include<iostream>
#include<iomanip>
using namespace std;


const int size=10;// use in menu[1] miles per gallon
typedef double *pointers;
void MilesPerrGallon(double *ptr1,double *ptr2);//process and display milespergallon
void MperG();//input miles and gallons and call function MilePerGallon
void getSort();//use dynamic array and a sort techniques to sort n values 
void displaySort(int *ptr,int smax);//displaying the vsorted values in descending order
char displayMenu(char *pt);//display the option on screen and return values 1,2 or 3 
//using a pointer variable

int  main()
{
   char ans,sagot;
   char *ptr;
   
//add code here
do{
	ptr=&sagot;
		do
		{
			ans=displayMenu(ptr);
		}while(*ptr!='1'&&*ptr!='2'&&*ptr!='3');
   
	switch(sagot)
	{
	//add code here
		case '1':  
		        {
					MperG();
				}
			break;
		case '2':  
				{			
					getSort();
				}
			break;
		case '3':
				{
		            cout<<"goodbye for now....\n";
		            system("pause");
		            exit(1);
				}
		            exit(1);
			break;
		 default:
				{
					cout<<"PAKYU";
				} 
			break;
	}
	
	
	do
	{
		cout<<"Try Again?[y/n]:";
		cin>>*ptr;
		*ptr=tolower(*ptr);
	}while(*ptr!='y'&&*ptr!='n');
	system("cls");
	}while(*ptr=='y');
	cout<<endl;
	cout<<"Thank you for using my program!";
	system("pause>0");
	return 0;
}
	

/////////////////////////
void MperG()
{double miles[size],gallons[size];
	int ctr;
	pointers milPtr,galPtr;
	int msize;

	system("cls");
	cout<<"COMPUTING FOR MPG : miles per gallon...\n";

	cout<<"MILES\n";
	//add code here
	while(true)
	{
	cout<<"Size must be from 5 to 15 only.\n\n";
	cout<<"Specify the size of the array: ";
	cin>>msize;
	try
		{
			if(msize>15||msize<3)
			 throw msize;
			 break;
		}
	catch(int x)
		{
			cout<<"\nSIZE MUST BE FROM 5 TO 15 ONLY!";
			system("pause>0");
			system("cls");
			continue;
		}
	}
	
	milPtr=new double[msize];
	galPtr=new double[msize];
	
	system("cls");
	cout<<"Miles\n";
	for(ctr=0;ctr<msize;ctr++)
	{
		bool ans=true;
		while(ans)
		{
			cout<<"\n\nThe number must be from 100-250 only.\n\n";
			cout<<"Miles ("<<ctr<<"): ";
			cin>>*(milPtr+ctr);

		try
			{
				if(*(milPtr+ctr)>250||*(milPtr+ctr)<100)
				 throw *(milPtr+ctr);
				 break;
			}
		catch(double e)
			{
				cout<<e<<" IS INVALID. THE NUMBER MUST BE FROM 100-250 ONLY!";
				system("pause>0");
				system("cls");
				continue;
			}
		}
	}


/*********************************************************/
cout<<"GALLONS\n";
	//add code here
system("cls");
cout<<"GALLONS\n";
cout<<"\n\nThe number must be from 5-25 only.\n\n";
	
	for(ctr=0;ctr<msize;ctr++)
	{
		bool ans=true;
		while(ans)
		{
			cout<<"Gallons ("<<ctr<<"): ";
			cin>>*(galPtr+ctr);

		try
			{
				if((*galPtr+ctr)>25||*(galPtr+ctr)<5)
				 throw *(galPtr+ctr);
				 break;

			}
		catch(double e)
			{
				cout<<e<<" IS INVALID. THE NUMBER MUST BE FROM 5-25 ONLY!";
				system("pause>0");
				system("cls");
				continue;
			}
		}
	}

//call MilePerrGallon here

}
////////////////////////
void MilesPerrGallon(double *ptr1,double *ptr2)
{

	double mpg[size];
	pointers mpgPtr;
   	 int index;
	//add code here
	system("cls");
	mpgPtr=&mpg[0];
	for(index=0;index<sz;index++)
		*(mpgPtr+index)=*(ptr1+index)/ *(ptr2+index);
	cout<<"MILES     /        GALLON     =   MPG\n";
	cout<<fixed<<setprecision(2); 
	for(index=0;index<sz;index++)
		{
			cout<<*(ptr1+index)
				<<setw(5)<<"/"
				<<setw(12)<<*(ptr2+index)
				<<setw(16)<<*(mpgPtr+index)
				<<endl;
		}
}
///////////////////////////////////////
void getSort()
{
	//add code here
	int srt;
	pointers srtPtr;
	int ssize;

	system("cls");
	cout<<"Sorting\n";
		cout<<"How many numbers would you like to sort? ";
		cin>>ssize;
	
	srtPtr=new double[ssize];
	cout<<"\n\n\nIndex\n";
	for(srt=0;srt<ssize;srt++)
	{
		bool ans=true;
		while(ans)
		{
			cout<<"Index ["<<srt<<"]: ";
			cin>>*(srtPtr+srt);

		try
			{
				if(*(srtPtr+srt)>250||*(srtPtr+srt)<100)
				 throw *(srtPtr+srt);
				 break;
			}
		catch(double e)
			{
				cout<<e<<" IS INVALID. THE NUMBER MUST BE FROM 100-250 ONLY!";
				system("pause>0");
				system("cls");
				continue;
			}
		}
	}


//call displaysort here

}


/////////////////////////
void displaySort(int *ptr,int smax)
{
	//add code here
	double srt[size];
	pointers srtPtr;
   	 int index;
   	 
	system("cls");
	srtPtr=&srt[0];
	cout<<"The "<<srt<<" numbers stored in decending order";
	for(index=0;index<smax;index++)
		*(srtPtr+index)=*(ptr+index);
	cout<<"Index ["<<srt<<"]: ";
	cout<<fixed<<setprecision(2); 
	for(index=0;index<smax;index++)
		{
			cout<<*(ptr+index)
				<<setw(5)<<"/"
				<<setw(16)<<*(srtPtr+index)
				<<endl;
		}
}
////////////////////////////////////////////

//////////////////////////////////////////
char displayMenu(char *pt)
{
	//add code here
	cout<<"MAIN MENU"<<endl
		<<"[1] - MPG (Miles Per Gallon)"<<endl
		<<"[2] - Sorting of Numbers"<<endl
		<<"[3] - Exit Program"<<endl
		<<"Enter Option: ";
	cin>>*pt;
   return *pt;
}
Last edited on
> when i tried to call (...) it always results of compile error.
when posting code, post the relevant parts. There is no point in posting code that does not cause the error.
when getting an error, post the full message. Verbatim.


your variable names are too "helpful" and "meaningful" to deduce their meaning.
MilesPerrGallon(miles, gallons);


About exceptions.
A function should throw an exception because it can't handle the situation (either don't have the knowledge or the resources to do it). The exception would propagate to the callers till somebody catches it and handled it.
I don't see the point in throwing if you are going to resolve it two lines below (like in line 150--157). As it is, there is no separation between the code that handles the normal flow and the exception one.
Last edited on
closed account (yAUfizwU)
thanks for the help and advice ne555. I appreciate it.
Topic archived. No new replies allowed.