Help with program please :)

closed account (ETAkoG1T)
This is the program that I have tried to make re evaluate values in an array. I can't really put my finger on what is wrong... Here is the code it generates some errors about the function but not very specific.

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
#include "stdafx.h"
#include <iostream>
const int Max = 5;
double * fill_array(double *ar, int limit);
void show_array(const double *ar,double  *end);
void revalue(double r, double *ar,double  *end);
using namespace std;
int main()
{
	double properties[Max];
	double *prop = &properties[0];
	double *size = fill_array(prop, Max);
	show_array(prop, size);
			double factor;
if(size > 0)
	{
		cout << "Enter revaluation factor: ";
		while(!(cin >> factor))
		{
			cin.clear();
			while(cin.get() != '\n')
				continue;
			cout << "Bad input; Please enter a number: ";
		}
	}
	revalue(factor, prop, size);
	show_array(properties, size);
	return 0;
}

int fill_array(double *ar, int limit)
{
	double *pt = ar;
	double temp;
	while(*pt < limit)
	{
		cout << "Enter value #" << pt << ": ";
		cin >> temp;
		if(!cin)
		{
			cin.clear();
			while(cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated. \n";
			break;
		}
		else if(temp < 0)
			break;
		ar[i] = temp;
		*pt += 1;
	}
	return * pt;
}

void show_array(const double *ar, double *end)
{
	using namespace std;
	double *pt = ar;
	while(*pt != *end)
	{
		cout << "property #" << pt << ": ";
		cout << *pt << endl;
		*pt += 1;
	}
} 

void revalue(double r, double *ar, double *end)
{
	double *pt = ar;
	while (*pt != *end)
	{
		*pt *= r;
	}
}
Good that you show the code and moreover, inside code tags, but you fail to explain what is wrong with this. What is the success criteria to be used when running/evaluating this code?
closed account (ETAkoG1T)
It has something to do with the functions not compatible with what they are used for... The problem is that I don't know exactly what the problem is :P
Just gave a rapid eye and spotted somethings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void show_array(const double *ar, double *end)
{
	using namespace std;
	while(at != end) // 1. You don't need to copy the pointer
	// 2. Don't compare values but pointers instead
	{
		cout << "property #" << pt << ": ";
		cout << *pt << endl;
		++pt; // Same over here, dont use values but ptrs
	}
} 

void revalue(double r, double *ar, double *end)
{
	while (ar != end) // Also here
	{
		*ar *= r; // You are multiplying the value I hope this is right
	}
}
closed account (ETAkoG1T)
Thanks I am a mess when it comes to pointers!
I get this error along with some other errors:
4 IntelliSense: cannot overload functions distinguished by return type alone

It is on line 4.
Last edited on
closed account (ETAkoG1T)
I fixed most of the problams, now it will compile and run but there is a program with show_array :/

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
// Ch7_7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
const int Max = 5;
double *fill_array(double *ar, int limit);
void show_array(double *ar,double  *end);
void revalue(double r, double *ar,double  *end);
using namespace std;
int main()
{
	double properties[Max];
	double *prop = &properties[0];
	double *size = fill_array(prop, Max);
	show_array(prop, size);
	double factor;
if(size > 0)
	{
		cout << "Enter revaluation factor: ";
		while(!(cin >> factor))
		{
			cin.clear();
			while(cin.get() != '\n')
				continue;
			cout << "Bad input; Please enter a number: ";
		}
	}
	revalue(factor, prop, size);
	show_array(prop, size);
	cin.get();
	cin.get();
	return 0;
}

double * fill_array(double *ar, int limit)
{
	int i = 0;
	double temp;
	while(i < limit)
	{
		cout << "Enter value #" << ar << ": ";
		cin >> temp;
		if(!cin)
		{
			cin.clear();
			while(cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated. \n";
			break;
		}
		else if(temp < 0)
			break;
		*ar = temp;
		i += 1;
	}
	return ar;
}

void show_array(double * ar, double *end)
{
	using namespace std;
	double *pt = ar;
	for(;pt != end;*pt++)
	{
		cout << "property #" << ar << ": ";
		cout << *ar << endl;
	}
} 

void revalue(double r, double *ar, double *end)
{
	while (ar != end) // Also here
	{
		*ar *= r; // You are multiplying the value I hope this is right
		ar++;
	}
}
See the fixes i posted above, they fix this problem. especially " *pt++ " should be " ++pt "
closed account (ETAkoG1T)
I have used what you told me but even though it does compile there is something wrong making show_array only show the last number in the array then the rest is only crazy random numbers... :/ Doesn't fix everything
closed account (ETAkoG1T)
I found my mistake :)
I forgot the ++ar in this while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	while(i < limit)
	{
		cout << "Enter value #" << ar << ": ";
		cin >> temp;
		if(!cin)
		{
			cin.clear();
			while(cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated. \n";
			break;
		}
		else if(temp < 0)
			break;
		*ar = temp;
		++ar;
	}
Last edited on
Topic archived. No new replies allowed.