-
May 21st, 2013, 01:39 PM
#1
return pointer
The code below is supposed to fill, show, and revalue property. The fill function is supposed to return a pointer that creates a range of property values. The show function is supposed to show the property values entered and the revalued property values. I think part of the problem is the returned pointer from the fill function. Once that is cleared up, I think I will find more problems.
Code:
#include <iostream>
const int Max = 5;
// function prototypes
double fill_array(double ar[], int limit);
void show_array(double * begin, double * end);
void revalue(double r, double ar[], const double * begin, const double * end);
int main()
{
using namespace std;
double properties[Max];
double pk = fill_array(properties, Max);
show_array(properties, &pk);
if (pk > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, properties, &pk);
show_array(properties, &pk);
}
cout << "Done.\n";
// cin.get();
// cin.get();
return 0;
}
double fill_array(double ar[], int limit)
{
using namespace std;
double temp;
double * pt;
int i;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << (i + 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;
ar[i] = temp;
}
pt = ar + i;
return *pt;
}
// the following function can use, but not alter,
// the array whose address is ar
void show_array(double * begin, double * end)
{
using namespace std;
const double * pt;
for (pt = begin; pt != end; pt++)
{
cout << "Property #" << (pt + 1) << ": $";
cout << pt << endl;
}
}
// multiplies each element of ar[] by r
void revalue(double r, double ar[], const double * begin, const double * end)
{
const double * pt;
double revalue = 0;
for (pt = begin; pt != end; pt++)
{
revalue = revalue * *pt;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|