buggy highest element program - help needed

It's intentionally buggy in the book. I need to fix it. But there's also another problem for 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
// Osman Zakir
// 11 / 10 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 20 "Find highest element" example code (with templated high() function)
// This program has a serious bug that I have to find and fix.

#include "../../cust_std_lib_facilities.h"
#include <iostream>
#include <vector>
#include <vld.h>

template<typename Iterator>
Iterator high(Iterator first, Iterator last);
double *get_from_jack(int *count);
std::vector<double> *get_from_jill();
void fct();

int main()
{
	fct();
	keep_window_open();
}

template<typename Iterator>
Iterator high(Iterator first, Iterator last)
	// return an iterator to the element in [first:last) that has the highest value
{
	Iterator high = first;
	for (Iterator p = first; p != last; ++p)
	{
		if (*high < *p)
		{
			high = p;
		}
	}
	return high;
}

double *get_from_jack(int *count)
{
	*count = 10;
	double *data_arr = nullptr;
	try
	{
		data_arr = new double[*count];
	}
	catch (const std::bad_alloc &e)
	{
		std::cout << "Bad allocation error: " << e.what() << '\n';
	}
	catch (const std::exception &e)
	{
		std::cout << "Exception: " << e.what() << '\n';
	}

	std::size_t i = 0;
	std::cout << "Please enter 10 velocitices or more velocities: enter anything that is not a number to stop:\n";
	for (double val; std::cin >> val;)
	{
		std::cin.ignore(32767, '\n');
		if (i >= static_cast<std::size_t>(*count))
		{
			double *new_arr = nullptr;
			try
			{
				new_arr = new double[*count + 1];
			}
			catch (const std::bad_alloc &e)
			{
				std::cout << "Bad allocation error: " << e.what() << '\n';
			}
			catch (const std::exception &e)
			{
				std::cout << "Exception: " << e.what() << '\n';
			}
			try
			{
				std::copy(data_arr, data_arr + *count, new_arr);
			}
			catch (const std::exception &e)
			{
				std::cerr << "Exception: " << e.what() << '\n';
			}
			data_arr = new_arr;
			delete[] new_arr;
			new_arr = nullptr;
		}
		data_arr[i] = val;
		++i;
	}
	std::cin.ignore(32767, '\n');
	std::cin.clear();
	std::cin.clear();
	*count = i;
	return data_arr;
}

std::vector<double> *get_from_jill()
{
	constexpr int size = 10;
	std::vector<double> *data_vec = nullptr;
	try
	{
		data_vec = new std::vector<double>(size);
	}
	catch (const std::bad_alloc &e)
	{
		std::cerr << "Bad alloction error: " << e.what() << '\n';
	}

	std::cout << "Please enter 10 velocitices or more velocities: enter anything that is not a number to stop:\n";
	for (double val; std::cin >> val;)
	{
		std::cin.ignore(32767, '\n');
		data_vec->push_back(val);
	}
	return data_vec;
}

void fct()
{
	int jack_count = 0;
	double *jack_data = get_from_jack(&jack_count);
	std::vector<double> *jill_data = get_from_jill();

	double *jack_high = high(jack_data, jack_data + jack_count);
	std::vector<double> &v = *jill_data;
	double *jill_high = high(&v[0], &v[0] + v.size());

	std::cout << "Jill's high " << *jill_high << "; Jack's high " << *jack_high << '\n';

        delete[] jack_data;
	jack_data = nullptr;
	delete jill_data;
	jill_data = nullptr;
}


The read loop for the vector won't run. And the program is giving wrong results with input like this:
100
45
63
74
79
250
150
12
13
14
15
16
17
, saying that the largest value is whatever I entered last. And sometimes it gives me a heap corruption error, too. And it even freezes after printing the results, sometimes.

fct() and high() were given in the book. I wrote main() and the functions for getting the data myself.

I think I know why the program gives wrong results: It depends on what the first number is. It expects the given sequence to be sorted, and gives wrong results otherwise.

Edit: The reason it freezes is because there's a breakpoint triggered by fct() line 141. It shows there's a problem with me calling delete[] there when I run the code in the debugger. But when I comment that line out, VLD shows I have a leak because I didn't free that memory. How do I fix this? (But yeah, I need to fix the problem with get_from_jill() first, for why the input loop won't run).

Edit2: Line 141 on my IDE and line 132 on here, for some reason.
Last edited on
Okay, never mind about the line number; my own updated code got longer. I'll post it here:
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
// Osman Zakir
// 11 / 10 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 20 "Find highest element" example code (with templated high() function)
// This program has a serious bug that I have to find and fix.

#include "../../cust_std_lib_facilities.h"
#include <iostream>
#include <vector>
#include <vld.h>

template<typename Iterator>
Iterator high(Iterator first, Iterator last);
double *get_from_jack(int *count);
std::vector<double> *get_from_jill();
void fct();

int main()
{
	try
	{
		fct();
		std::cin.ignore(32767, '\n');
		std::cin.clear();
	}
	catch (const std::exception &e)
	{
		std::cerr << "Exception: " << e.what() << '\n';
	}
	keep_window_open();
}

template<typename Iterator>
Iterator high(Iterator first, Iterator last)
	// return an iterator to the element in [first:last) that has the highest value
{
	Iterator high = first;
	for (Iterator p = first; p != last; ++p)
	{
		if (*high < *p)
		{
			high = p;
		}
	}
	return high;
}

double *get_from_jack(int *count)
{
	*count = 10;
	double *data_arr = nullptr;
	try
	{
		data_arr = new double[*count];
	}
	catch (const std::bad_alloc &e)
	{
		std::cout << "Bad allocation error: " << e.what() << '\n';
	}
	catch (const std::exception &e)
	{
		std::cout << "Exception: " << e.what() << '\n';
	}

	std::size_t i = 0;
	std::cout << "Please enter 10 velocitices or more velocities: enter anything that is not a number to stop:\n";
	for (double val; std::cin >> val;)
	{
		std::cin.ignore(32767, '\n');
		if (i >= static_cast<std::size_t>(*count))
		{
			double *new_arr = nullptr;
			try
			{
				new_arr = new double[*count + 1];
			}
			catch (const std::bad_alloc &e)
			{
				std::cout << "Bad allocation error: " << e.what() << '\n';
			}
			catch (const std::exception &e)
			{
				std::cout << "Exception: " << e.what() << '\n';
			}
			try
			{
				std::copy(data_arr, data_arr + *count, new_arr);
			}
			catch (const std::exception &e)
			{
				std::cerr << "Exception: " << e.what() << '\n';
			}
			data_arr = new_arr;
			delete[] new_arr;
			new_arr = nullptr;
		}
		data_arr[i] = val;
		++i;
	}
	std::cin.ignore(32767, '\n');
	std::cin.clear();
	std::cin.clear();
	*count = i;
	return data_arr;
}

std::vector<double> *get_from_jill()
{
	constexpr int size = 10;
	std::vector<double> *data_vec = nullptr;
	try
	{
		data_vec = new std::vector<double>(size);
	}
	catch (const std::bad_alloc &e)
	{
		std::cerr << "Bad alloction error: " << e.what() << '\n';
	}

	std::cout << "Please enter 10 velocitices or more velocities: enter anything that is not a number to stop:\n";
	double val = 0;
	while (std::cin >> val)
	{
		data_vec->push_back(val);
	}
	return data_vec;
}

void fct()
{
	int jack_count = 0;
	double *jack_data = get_from_jack(&jack_count);
	std::vector<double> *jill_data = get_from_jill();

	double *jack_high = high(jack_data, jack_data + jack_count);
	std::vector<double> &v = *jill_data;
	double *jill_high = high(&v[0], &v[0] + v.size());

	std::cout << "Jill's high " << *jill_high << "; Jack's high " << *jack_high << '\n';

        delete[] jack_data;
	jack_data = nullptr;
	delete jill_data;
	jill_data = nullptr;
}
Last edited on
Topic archived. No new replies allowed.