Pointer Problems

My test runs but I don't think I am using my pointers right but after days of trying to figure it out I need new eyes to see where I am going wrong. I have a random number generator that creates resistor values and then stores them in an array, but when I go to sort them the values are not the same as the first time.... Please help

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include<stdio.h>
using std:: cout;
using std:: cin;
using std::endl;

using namespace std;

enum pResistors {R_nominalValue,R_One,R_Two,R_Three};

class Resistor
{
public:
	static int ResCount;

	Resistor(void);//constructor
	Resistor(double);
	void setNominalResistance(double);
	double getNominalResistance();
	void SortObj(Resistor **);
	void displayMenu(void);
	char getMenuSelection(void);
	~Resistor(void);//destructor
	double pResistor[4];

private:
	double nominalResistance;
};

Resistor::Resistor(void) //constructor
{
}

Resistor::Resistor(double nomRes)
{
	pResistor[4];
	nominalResistance = nomRes;
	ResCount = ResCount + 1;
	if(rand()%10000+1000 < 1000 || rand()%10000+1000 > 10000)
	{
		ResCount = ResCount - 1;
	}
}
int Resistor::ResCount = 0;
void Resistor::setNominalResistance(double nomRes)
{
	nominalResistance = nomRes;
}

double Resistor::getNominalResistance()
{
	return nominalResistance;
}


Resistor::~Resistor(void) //destructor
{
}

void displayMenu()
{

	cout << " Enter C  to create a resistor " << endl;
	cout<< endl;

	cout << " Enter S to sort results " << endl;
	cout << endl;
	cout << " Enter Q To quit " << endl;
	cout << endl;
}

char getMenuSelection() //Gets the menu selection
{
	cout << "Enter your choice and press enter:";
	char Choice;
	cin >> Choice;

	switch(Choice)
	{
	case 'c':
	case 'C':
		return Choice;
		break;
	case 's':
	case 'S':
		return Choice;
		break;
	case 'q':
	case 'Q':
		exit(0);
		break;
	default:	//if menu selection is not valid let the user know
		cout << "\nInvalid selection: try again" << endl;
		displayMenu();
		getMenuSelection();
		break;

	}
}

void SortObj(Resistor **R)
{
	int i, j, mark = 1;    // set marker to 1 for the first pass
	Resistor *temp; //pointer to help swap pointers

	for(i = 1; (i <= 4) && mark; i++)
	{
		mark = 0;
		for (j=0; j < (3); j++)
		{
			if (R[j + 1] -> getNominalResistance() < R[j] -> getNominalResistance())
			{
				temp = R[j];
				R[j] = R[j + 1];
				R[j + 1] = temp;
				mark = 1;
			}
		}
	}
	delete temp;

}
void main()
{
	Resistor *pResistor[4];
	 srand(time(0));
	int i = 0;
	char Choice;




	do
	{
		displayMenu(); //displays the intitial menu
		Choice = getMenuSelection(); //gets the menu selection
		if(Choice == 'c' || Choice == 'C')
		{
			if(Resistor::ResCount == 4)
			{
				cout << "you entered the maximum number of 4 resistors." << endl;
			}
			do
			{
				cout << "Your random resistance must be between 1000 and 10000" << endl;
				if(Resistor::ResCount < 4)
				{
					cout  <<"Resitor"<< i + 1 << "   Random Resistance is:"<<endl;
					cout<< rand()%10000+1000;
					cout<<endl;


					pResistor[i] = new Resistor(rand()%10000+1000);
				}
			}
			while(rand()%10000+1000 < 1000 || rand()%10000+1000 > 10000);
			i++;
		}

		else if(Choice == 's' || Choice == 'S')
		{
			if(Resistor::ResCount < 4)
			{
				cout << "You have not used the maximum of 4 resistors" << endl;
			}
			else
				SortObj(&pResistor[0]);
			for(int j = 0; j < Resistor::ResCount; j++)
			{
				cout  << j + 1 << "\n";
				cout << "Nominal Resistance:" << pResistor[j] -> getNominalResistance() << "\n";

			}
		}
	}
	while(Choice != 'q' || Choice != 'Q'); //loop until quit

	delete [] pResistor;
}

Topic archived. No new replies allowed.