### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184`` ``````#include #include #include #include #include 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:"< 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.