Trouble with user input in OOP

This program will be used to calculate the voltage and the current. It needs to have an interactive menu where the user can freely choose.

i am thinking the output to be something like this:

Please choose from following options:
1 - Set Resistance
2 - Change Voltage
3 - Change Current
4 - Exit
1
Please input new resistance value: 5
Resistance is now 5(Ohm).

Please choose from following options:
1 - Set Resistance
2 - Change Voltage
3 - Change Current
4 - Exit
2
Please input new voltage value: 20

************************************
Resistance is now 5(Ohm).
Voltage is now 20(V).
Current is now 4(A).
************************************

Please choose from following options:
1 - Set Resistance
2 - Change Voltage
3 - Change Current
4 - Exit
3
Please input new current value: 17

************************************
Resistance is now 5(Ohm).
Voltage is now 85(V).
Current is now 17(A).
************************************
Please choose from following options:
1 - Set Resistance
2 - Change Voltage
3 - Change Current
4 - Exit
4


It has to be done using OOP but i'm not that familiar with that.
So I have declared the class and designated public and private control access, and set up the class functions. But on my main part of the program I am having problems allowing the user to insert the voltage value.

Help me please, my head is frying right now :D

Code:
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
  //this program will have a interactive menu to set and change current, resistor resistance and voltage
#include<iostream>
#include<string.h>

using namespace std;

class Resistor{
private: 
	double resist_value;
	double voltage_value;
	double current_value;
	char resistor_unit;
	char voltage_unit;
	char current_unit;
public:
	double setResist_value();
	void setVoltage_value(double);
	void setCurrent_value(double);
	double getResist_value() const;
	double getVoltage_value() const;
	double getCurrent_value() const;

	Resistor(); //resistor default constructor
	Resistor(double resistValue); // Overloaded constructor
	~Resistor();

	//Setting up resistor values
	Resistor::Resistor() {resist_value = 20.0;} //Setting up resistor default constructor value to 20 OHMs
	Resistor::Resistor(double resistValue) { resistValue = resist_value; } //Allowing resistor to have a custom value by overloading the constructor
	
	//Setting up Resistor functions
	double Resistor::getResist_value() { voltage_value / current_value;}
	double Resistor::getVoltage_value() { current_value * resist_value;}
	double Resistor::getCurrent_value() { voltage_value / resist_value;}
	double Resistor::setResist_value();
	double Resistor::setVoltage_value();
	double Resistor::setCurrent_value();
	
int main()
{
	int option; // the user option is going to be stored in this variable
	do // do-while loop start. The loop will continue until the user press the "EXIT" option
	{
		// menu display start
		cout << "1- Set Resistance value: " << endl;
		cin >> setResist_value;
		cout << "2- Change Voltage value: " << endl;
		cout << "3- Change Current value:  " << endl;
		cout << "4- Exit " << endl;
Tried to get something to compile best effort below.
Had to comment out , ~Resistor(), otherwise there was an error on the return 0; line 81.
Don't know why. Still learning this stuff myself.
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
#include <iostream>

using namespace std;

  //this program will have a interactive menu to set and change current, resistor resistance and voltage
#include<iostream>
#include<string.h>

using namespace std;

class Resistor{
private:
	double resist_value;
	double voltage_value;
	double current_value;
	char   resistor_unit;
	char   voltage_unit;
	char   current_unit;
public:
	void   setResist_value();
	void   setVoltage_value();
	void   setCurrent_value();
	double getResist_value();
	double getVoltage_value();
	double getCurrent_value();

	Resistor(); //resistor default constructor
	Resistor(double resistValue); // Overloaded constructor
	//~Resistor();
};

	//Setting up resistor values
	Resistor::Resistor() {resist_value = 20.0;} //Setting up resistor default constructor value to 20 OHMs
	Resistor::Resistor(double resistValue) { resistValue = resist_value; } //Allowing resistor to have a custom value by overloading the constructor

	//Setting up Resistor functions
	double Resistor::getResist_value(){return voltage_value / current_value;}
	double Resistor::getVoltage_value() { return current_value * resist_value;}
	double Resistor::getCurrent_value() { return voltage_value / resist_value;}
	void Resistor::setResist_value()
	{
	    cout << "Enter Resistence :";
	    cin  >> resist_value;
	}
	void Resistor::setVoltage_value()
	{
	    cout << "Enter Voltage: ";
	    cin  >> voltage_value;
	}

	void Resistor::setCurrent_value()
	{
	    cout << "Enter Current";
	    cin  >> current_value;
	}

int main()
{
	int option; // the user option is going to be stored in this variable

	Resistor resistor1;  // create an object

	do // do-while loop start. The loop will continue until the user press the "EXIT" option
	{
		// menu display start
		cout << "1- Set Resistance value: " << endl;
		cout << "2- Change Voltage value: " << endl;
		cout << "3- Change Current value:  " << endl;
		cout << "4- Exit " << endl;
        cin  >> option;
        if (option == 1)
            resistor1.setResist_value();
        if (option == 2)
            resistor1.setVoltage_value();
        if (option == 3)
            resistor1.setCurrent_value();

	}while (option != 4);


    return 0;
}

Last edited on
@CodeWriter thanks with your help i could finalize the code.
Maybe it was only complicated because. I've just added functions to the menu to be interactive.
Well her's how it is now:
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
#include <iostream>

using namespace std;

//this program will have a interactive menu to set and change current, resistor resistance and voltage
#include<iostream>
#include<string.h>

using namespace std;

class Resistor{
private:
	double resist_value;
	double voltage_value;
	double current_value;
	char   resistor_unit;
	char   voltage_unit;
	char   current_unit;
public:
	void   showResult();
	void   setResist_value();
	void   setVoltage_value();
	void   setCurrent_value();
	double getResist_value();
	double getVoltage_value();
	double getCurrent_value();

	Resistor(); //resistor default constructor
	Resistor(double resistValue); // Overloaded constructor
	
};

//Setting up resistor values
Resistor::Resistor() { resist_value = 20.0; } //Setting up resistor default constructor value to 20 OHMs
Resistor::Resistor(double resistValue) { resistValue = resist_value; } //Allowing resistor to have a custom value by overloading the constructor

//Setting up Resistor functions
double Resistor::getResist_value(){ return voltage_value / current_value; }
double Resistor::getVoltage_value() { return current_value * resist_value; }
double Resistor::getCurrent_value() { return voltage_value / resist_value; }

 // Resistance Setting
void Resistor::setResist_value()
{
	cout << "Please input a new Resistance value: ";
	cin >> resist_value;
	if (resist_value <= 0)
		cout << "ERROR: THE NUMBER YOU HAVE INPUT IS NEGATIVE, PLEASE INPUT A POSITIVE NUMBER" << endl;
	else if (resist_value > 0)
		cout << " The new Resistance is set up to: " << resist_value << " ohms" << endl;
	cout << "********************************" << endl;
}
	// Current Calculations
void Resistor::setVoltage_value()
{
	cout << "Input the Voltage value to calculate the Current:  ";
	cin >> voltage_value;
	current_value = voltage_value / resist_value;
	cout << " The Current that passes through the Resistor is: " << current_value << " Amperes" << endl;
	cout << "********************************" << endl;

}
		// Voltage Calculations
void Resistor::setCurrent_value()
{
	cout << "Input the Current value to calculate the Voltage: ";
	cin >> current_value;
	voltage_value = current_value * resist_value;
	cout << " The Voltage across the Resistor is: " << voltage_value << " Volts" << endl;
	cout << "********************************" << endl;
	
}

int main()
{
	int option; // the user option is going to be stored in this variable

	Resistor resistor1;  // create an object

	do // do-while loop start. The loop will continue until the user press the "EXIT" option
	{
		// menu display start
		cout << "NOTE: The default value for the Resistance is 20 Ohms." << endl;
		cout << "1- Set Resistance value " << endl;
		cout << "2- Change Voltage value " << endl;
		cout << "3- Change Current value  " << endl;
		cout << "4- Exit " << endl;
		cin >> option;
		if (option > 4)
			cout << "The option that you have chosen is incorrect. Please choose a number between 1-4" << endl;
		if (option == 1)
			resistor1.setResist_value();
		if (option == 2)
			resistor1.setVoltage_value();
		if (option == 3)
			resistor1.setCurrent_value();

	} while (option != 4);


	return 0;
}

@CodeWriter

in line 29 put

~Resistor() { } // You have forgotten the braces


or

uncomment it .. and write after the class..
1
2
3
4
5
6
7

Resistor::~Resistor()
{

}

@obscure,
Of course thanks for pointing that out.
My understanding is that if you leave the destructor out the compiler will insert one.
And I also understand the destructor may be required to clean up when the object is deleted.

Topic archived. No new replies allowed.