terminate called after throwing an instance of 'double' (help)

Hello, I am just a beginner at C++ and I have a question...

How can I resolve this problem? When I compile my code it seems fine but when I always input a number, it always say (terminate called after throwing an instance of 'double')
Any help would be appreciated!


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
#include<iostream>
#include<iomanip>
const int size=10;
typedef double *pointers;
	
using namespace std;

int main(){

	double miles[size],gallons[size];
	int ctr;
	pointers milPtr,galPtr;
	milPtr = new double[size];
	galPtr = new double[size];
	ctr = 0;
	while(ctr < size)
	{
		try
		{
			cout <<"mile" << "[" << ctr << "]" << setw(3);
			cin >> milPtr[ctr];
			ctr++;
			mailiysu = false;
			if(milPtr[ctr]< 100 || milPtr[ctr] > 250)
			{
				throw milPtr[ctr];
			}
	
		}
		catch(pointers milPtr)
		{
			cout << milPtr[ctr]<< " is invalid!.. 100-250 only" << endl;
			continue;
		}
	}
}
You are incrementing your counter after you get the array element from the user and before you are checking the input. You get input for milPtr[0] and then are checking milPtr[1]. That is going to be a garbage value that is likely to be outside the range.
Well, you are throwing a double, but not catching it because what you are set up to catch is a double*.

Apart from that:
- your code doesn't compile (you haven't declared mailiysu);
- you aren't using quite a lot of your variables;
- ctr++ is in the wrong place - it should be after your check on range

And why do you need new when you know precisely how large your arrays are?

To be honest, why do you need try...catch here at all? You could just use an if...else
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
const int size=10;

int main()
{
   int ctr = 0;
   double milPtr[size];
   while( ctr < size )
   {
      cout << "mile" << "[" << ctr << "] ";
      cin >> milPtr[ctr];
      if ( milPtr[ctr] < 100 || milPtr[ctr] > 250 )
      {
         cout << milPtr[ctr]<< " is invalid!.. 100-250 only\n";
      }
      else
      {
         ctr++;
      }
   }
}
Last edited on
Hello NoobCoder2333,

First if you are going to use "new" to create memory on the heap you MUST have a "delete" to free the memory when you are done with it. Otherwise you have a memory leak. DO NOT count on the program ending to free this memory.

Line 10 defines 2 arrays on the stack that are never used.

Line 23 uses a variable that was never defined.

Line 22 increases "ctr" to soon. So in line 24 you are checking the next element of the array and not the 1 that you just entered a value into.

I would say that your error problem comes from going past the end of your array.

Line 17 can be eliminated if you initialize you variable when you define it.

I will know more when I have a chance to put this through a proper debugger.

Andy
Look at how you're inc ctr and when you use ctr. Also, you're not throwing a type pointers, you're throwing a type double! Consider:

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

const size_t asize {10};
typedef double* pointers;

int main()
{
	int ctr {};
	pointers milPtr {new double[asize] {}};
	pointers galPtr {new double[asize] {}};

	while (ctr < asize)
		try {
		cout << "mile" << "[" << ctr << "]" << setw(3);
		cin >> milPtr[ctr];

		if (milPtr[ctr] < 100 || milPtr[ctr] > 250)
			throw milPtr;
		else
			++ctr;
	}
	catch (pointers milPtr)
	{
		cout << milPtr[ctr] << " is invalid!.. 100-250 only" << endl;
	}

	delete[] milPtr;
	delete[] galPtr;
}

Last edited on
This is just a part of the file but I did delete it in the main function.
@lastchance I already did the one with a do-while loop then, this week's topic is exception handling.

Anyway, I am having trouble with the given problem, but thank you for the advices. I highly appreciated it!
Topic archived. No new replies allowed.