currently working on creating a function within a class that will receive colors on a resistor from user input (cin) and convert to resistance and tolerance.
The formula for resistance is R = AB x 10^C. Tolerance (D) is based on gold =5%, silver = 10%, none = 20%.Coding as follows:
for AB,C =
Black = 0;
Brown = 1;
Red = 2;
Orange = 3;
Yellow = 4;
Green = 5;
Blue = 6;
Violet = 7;
Gray = 8;
White = 9;
Silver = -1;
Gold = -2;
None = 20
there should be no cout within the function, print out should be through main.
so far i have:
class Resistance
{
private:
double Resistance, Tolerance;
string A, B, C, D;
public:
//------------------------------------------------------------------------------
int main()
{
system("pause");
return 0;
}
As you can see other functions include converting resistance and tolerance to colors, finding max and min resistance and standard resistance value from resistance and tolerance.
Any comments on how to tackle this thing would be appreciated. Thanks.
//------------------------------------------------------------------------------
int main()
{
cout <<" Please enter '1' or '2' based on the options below: " << endl;
cout <<"\n 1 for determining resistance and tolerance based on color bands,"<<
<<"as well as max and min resistance.";
cout <<"\n 2 for determining color bands based on resistance and tolerance,"<<
<<" as well as closest standard resistance value.";
cin >> opselect;
switch (opselect)
{
case 1:
cout <<"You have chosen to classify the resistance and tolerance of your"<<
<<" resistor based on color bands." <<
<<"Please check the colors carefully and input the colors from"<<
<<" first to last." << endl;
cout <<"The color of the first band (A): " << endl;
cin >> A;
cout <<"The color of the first band (B): " << endl;
cin >> B;
cout <<"The color of the first band (C): " << endl;
cin >> C;
cout <<"The color of the first band (D): " << endl;
cin >> D;
ColortoRT(A,B,C,D); //Call function
break;
case 2:
cout <<"You have chosen to classify the color bands of your resistor"<<
<<"based on resistance and tolerance." <<
<<"Please make sure the input is correct before entering."<<endl;
cout <<"The value of resistance: " << endl;
cin >> Resistance;
cout <<"The value of tolerance: " << endl;
cin >> Tolerance;
There are a couple of things that need to be taken care of.
-> First, you have many undeclared variables in main. In c++ you must declare variables before you use them. Notice that your class definition doesn't automatically declare anything. Your main should look more like this:
int main()
{
int opselect; //declare the opselect variable
Resistance res; //declare a variable of type "Resistance"
int resistance; //not "int Resistance" because "Resistance" is the name of your class!
int tolerance;
//cout statements...
cin >> opselect;
//...
cin >> res.A;
//...
cin >> resistance;
//...
system("pause");
return 0;
}
-> Second, you have to make the data members of your class public if you want to access them like that inside main. Alternatively, you could use a struct instead of a class (the default access modifier for a struct is public).
-> Third, since you store these things (A,B,C,D) as data members in your class, you don't have to pass them as arguments to your member functions.
My guess is that you just started learning about classes. Maybe doing some more reading will help:
ok i guess where i feel like im hitting a wall is, calling the function and then knowing what to do with it. what should i use to make this function work? for loops, arrays...? also if i want the return type to be pass by reference, BUT i also want a return type int for error testing WITHIN the member function how do i tackle that?
thanks.