A programming exercise I'm doing requires that a program be written whereby functions are used to 1) fill an array, 2) show the array and 3) alter the data of the array. The arrays are to be passed as pointers to the functions and the use of const keyword be applied where appropriate.

Here is my solution. I don't understand why I get a compiler error when making the arguments to the show_array() prototype and header as const . The compiler error message is:

"invalid conversion from 'const double*' to 'double'

and refers to the for loop (line 68) of the show_array function. For testing and debugging, I removed all references to the const keyword for the show_array function and the program works.

// chap07-q07.cpp  --  array functions using pointers
// C++ Primer Plus, Fifth Edition
// Chapter 7, Page 335.
// Programming Exercise # 7
// Modifies example, Listing 7.7 page 300.
// 13 Dec, 2007.

#include <iostream>
const int Max = 5;
using namespace std;

//function prototypes
double * fill_array(double * begin,double * end);
void show_array(const double * begin, const double * end);
void revalue(double r, double *begin, double *end);

int main()
{
	double properties[Max] = {0.0};
	double *last = fill_array(properties, properties + Max);
	show_array(properties, last);
	cout << "Enter revaluation factor: ";
	double factor;
	cin >> factor;
	revalue(factor, properties, last);
	show_array(properties, last);
	cout << "Done.\n";
	
	cout << "\n\n...Press any key to EXIT...";
	while (cin.get() != '\n')
		continue;
	cin.get();
	return 0;
}

double * fill_array(double * begin, double * end)
{
	double temp;
	double *i;
	int counter;
	for (i = begin, counter = 0; i != end; i++, counter++)
	{
		//cout << "Enter value # " << (i+1) << ": ";
		cout << "Enter value # " << counter + 1 << ": ";
		cin >> temp;
		if (!cin)    //bad input
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated.\n";
			break;
		}
		else if (temp < 0)    // signal to terminate
			break;
		*i = temp;
	}
	return i;
}

// the following function can use, but not alter
// the array - pointer

void show_array(const double *begin, const double *last)
{
	double *pt;
	int counter;
	for (pt = begin, counter = 0; pt != last; pt++, counter++)
		cout << "Property # " << counter + 1 << ": $" << *pt << endl;
}

// multiplies each element of pointer double by r

void revalue(double r, double *begin, double *last)
{
	double *ptd;
	for (ptd = begin; ptd != last; ptd++)
		*ptd *= r;
}

As far as I can see, the show_array doesn't change the contents of the passed in arguments.

Any clues or hints please.

>"invalid conversion from 'const double*' to 'double'
You mean "invalid conversion from 'const double*' to 'double*''". It's a significant difference, and the error is telling you that you're throwing away the const qualifier without a cast, which is illegal. It's coming from assigning begin (a pointer to const double) to pt (a pointer to double). Change this:

double *pt;
const double *pt;

>"invalid conversion from 'const double*' to 'double'
You mean "invalid conversion from 'const double*' to 'double*''". It's a significant difference, and the error is telling you that you're throwing away the const qualifier without a cast, which is illegal. It's coming from assigning begin (a pointer to const double) to pt (a pointer to double). Change this:

double *pt;
const double *pt;

Thank, that works and makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.