PLEASE H3LP: Converting color bands(strings) to resistance and tolerance

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:




};

//------------------------------------------------------------------------------

void Resistance::ColortoRT(string& A,string& B,string& C,string& D)
{
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

if (D == Gold)
{ D = 5;
if (D == Silver)
{ D = 10;
}


{



}





}

void Resistance::RTtoColor(double& R, double& T)
{


}

void Resistance::MaxandMin(double& r1, double& r2)
{

}


void Resistance::StanRes(double& R1, double& R2)
{


}



//------------------------------------------------------------------------------
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.


Last edited on
I have worked more on this..i now have:


//------------------------------------------------------------------------------
class Resistance
{
private:
double Resistance, Tolerance;
string A, B, C, D;
public:




};

//------------------------------------------------------------------------------

void Resistance::ColortoRT(string& c1,string& c2,string& c3,string& c4)
{
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

if (D == Gold)
{ D = 5;
if (D == Silver)
{ D = 10;
}


{



}





}

void Resistance::RTtoColor(double& R, double& T)
{


}

void Resistance::MaxandMin(double& r1, double& r2)
{

}


void Resistance::StanRes(double& R1, double& R2)
{


}



//------------------------------------------------------------------------------
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;




break;


} // end of switch

system("pause");
return 0;
}






still unsure of where to do from here.
Mmmm... Let's see what we have here...

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

http://cplusplus.com/doc/tutorial/classes/

As for the string->int conversion, you could use std::map<string,int> like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <map>
#include <string>
using namespace std;

map<string,int> color_to_num;

void init()
{
    color_to_num["black"]=0;
    color_to_num["brown"]=1;
    //etc...    
}

//...

int main()
{
    init();
    //...
}

Info on map -> http://cplusplus.com/reference/stl/map/
Last edited on
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.
That's what I have in mind:

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
#include <iostream>
#include <cmath> //for pow
#include <string>
#include <map>
using namespace std;

map<string,int> color_to_num_R;
map<string,int> color_to_num_T;

void init()
{
    color_to_num_R["black"]=0;
    color_to_num_R["brown"]=1;
    color_to_num_R["red"]=2;
    color_to_num_R["orange"]=3;
    color_to_num_R["yellow"]=4;
    color_to_num_R["green"]=5;
    color_to_num_R["blue"]=6;
    color_to_num_R["violet"]=7;
    color_to_num_R["gray"]=8;
    color_to_num_R["white"]=9;

    color_to_num_T["gold"]=5;
    color_to_num_T["silver"]=10;
    color_to_num_T["none"]=20;
}

struct Resistance
{
    double resistance;
    double tolerance;
    string A,B,C,D;

    //return int to indicate errors
    int color_to_res();

    //...
};

int Resistance::color_to_res()
{
    map<string,int>::iterator it;

    //check if A,B,C,D have valid values

    it=color_to_num_R.find(A);
    if (it==color_to_num_R.end())
        return 1;

    it=color_to_num_R.find(B);
    if (it==color_to_num_R.end())
        return 2;

    it=color_to_num_R.find(C);
    if (it==color_to_num_R.end())
        return 3;

    it=color_to_num_T.find(D);
    if (it==color_to_num_T.end())
        return 4;

    resistance=color_to_num_R[A]*10;
    resistance+=color_to_num_R[B];
    resistance*=pow(10.0,color_to_num_R[C]);

    tolerance=color_to_num_T[D]/100.0;

    return 0;
}

//...

int main()
{
    init();

    //...
}

Last edited on
if i keep the format that i originally have, how would i go about it? not to be difficult or anything.
I'm sure you can figure this one out yourself with some help from here -> http://cplusplus.com/doc/tutorial/
im never taking c++ again..i hate this.
i cannot use a struct i have to use a class...and i cannot use global variables. boooo..
jewing87 wrote:
i cannot use a struct i have to use a class...

Do it like this then:

1
2
3
4
5
6
7
class Resistance
{
public:

    //...

};

jewing87 wrote:
i cannot use global variables

Make color_to_num_R, color_to_num_T and init() static members of your class.

jewing87 wrote:
boooo..

This won't get you what you want.
Topic archived. No new replies allowed.