I am to make a program that finds the value of a resistor after inputting 3 letters those letters of are of the color on the resistor. i have written a program to do so but the value does not follow the equation. I am not sure if the program is not running because i am not used to writing programs using functions

p.s. sry if the program does not come out correct when i post

#include <iostream>
#include <cmath>
using namespace std;
int cResist(int,int,int);
int cCode(int);
int CC(int);
int c1,c2,c3;
double val;
int main(){
    cout<<"Color code""\t""Character code"<<endl;
        cout<<"Black""\t""B""\t""0"<<endl;
        cout<<"Brown""\t""N""\t""1"<<endl;
        cout<<"Red""\t""R""\t""2"<<endl;
        cout<<"Orange""\t""O""\t""3"<<endl;
        cout<<"Yellow""\t""Y""\t""4"<<endl;
        cout<<"Green""\t""G""\t""5"<<endl;
        cout<<"Blue""\t""E""\t""6"<<endl;
        cout<<"Violet""\t""V""\t""7"<<endl;
        cout<<"Gray""\t""A""\t""8"<<endl;
        cout<<"White""\t""W""\t""9"<<endl;
char c1,c2,c3;
    cout<<"input the first letter of the color:";cin>>c1;
    cout<<"input the second letter of the color:";cin>>c2;
    cout<<"input the third letter of the color:";cin>>c3;
    val=cResist(c1,c2,c3);
    cout<<val<<endl;
    return 0;
}
int cResist(int c1,int c2, int c3){
int val1,val2,val3;
    val1=cCode(c1);
    val2=cCode(c2);
    val3=cCode(c3);
    val=(10.0*val1+val2)*(pow(10.0,val3));
        return val;
}
int cCode(int CC){
    switch(CC){
        case 'B':0;break;
        case 'N':1;break;
        case 'R':2;break;
        case 'O':3;break;
        case 'Y':4;break;
        case 'G':5;break;
        case 'E':6;break;
        case 'V':7;break;
        case 'A':8;break;
        case 'W':9;break;
    }
    return CC;
}

Recommended Answers

All 6 Replies

you have c1,c2,c3 declared in global as int then inside the fn as char and then again as int... what r u trying to do? you declare them as char then you pass them to cResist which takes int, so it has the ASCII values and then you pass them to another fn and use them in a switch ... i think the first step should be to understand what kind of var you need and declare them properly..

I think so... because some guy in my class told me about the sight... I looked over his program just now and i can't figure out why mine runs into a wall when his doesn't. Mine will build but the answer for E.R.O. comes out as 7.72e+081 when it should be 62000. i changed the code around a couple of times but what ever i do it seems to give me an answer with some # *e + some other numbers.
i got rid of the global int for c1, c2, c3... and if i change the char within int main(line 13) into a double or int and make adjustments to the rest of the program it makes the program hit a wall faster.

1. int main(){
2.  cout<<"Color code""\t""Character code"<<endl;
3.          cout<<"Black""\t""B""\t""0"<<endl;
4.      cout<<"Brown""\t""N""\t""1"<<endl;
5.      cout<<"Red""\t""R""\t""2"<<endl;
6.      cout<<"Orange""\t""O""\t""3"<<endl;
7.      cout<<"Yellow""\t""Y""\t""4"<<endl;
8.      cout<<"Green""\t""G""\t""5"<<endl;
9.      cout<<"Blue""\t""E""\t""6"<<endl;
10.     cout<<"Violet""\t""V""\t""7"<<endl;
11.     cout<<"Gray""\t""A""\t""8"<<endl;
12.     cout<<"White""\t""W""\t""9"<<endl;
13. char c1,c2,c3;
14. cout<<"input the first letter of the color:";cin>>c1;
15. cout<<"input the second letter of the color:";cin>>c2;
16. cout<<"input the third letter of the color:";cin>>c3;
17. val=cResist(c1,c2,c3);
18. cout<<val<<endl;
19. return 0;
20.}
21. double cResist(double c1,double c2,double c3){
22.    int val1,val2,val3;
23. val1=cCode(c1);
24. val2=cCode(c2);
25. val3=cCode(c3);
26. val=((10.0*val1)+val2)*(pow(10.0,val3));
27.     return val;
28.}
29.int cCode(int CC){
30. switch(CC){
40.     case 'B':0;break;
41.     case 'N':1;break;
42.     case 'R':2;break;
43.     case 'O':3;break;
44.     case 'Y':4;break;
45.     case 'G':5;break;
46.     case 'E':6;break;
47.     case 'V':7;break;
48.     case 'A':8;break;
49.     case 'W':9;break;
50. }
51. return CC;
52.}

p.s. so far i have no real grasp on fn but i have tried simpler programs that i have already written for homework and done them again for practice but so far none have worked; even though i have done them before.

Sry wording was wrong on the program above so it would cause confusion

13. char c1,c2,c3;
14.	cout<<"input the first letter of the 1st color:";cin>>c1;
15.	cout<<"input the first letter of the 2nd color:";cin>>c2;
16.	cout<<"input the first letter of the 3rd color:";cin>>c3;
17.	val=cResist(c1,c2,c3);

p.s. i have reduced the amount of warnings from 3 to 1 by only using int instead of doubles but there is still a warning about (c:\users\owner\documents\visual studio 2005\projects\hw for 2-15-08\hw for 2-15-08\resistors.cpp(33) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data)
but those adjustments just give the wrong answer with negative

A lot of these issues were brought up in the other threads. Apparently very high resistor values are above 2 billion and so overflow the integer bounds. Another poster suggested storing the values, even though they are really integers, as doubles, since doubles use 64 bits instead of 32 and may be able to store larger numbers. I think that's probably true, but I don't recall which values doubles can store. I know that floats cannot store all the values that integers can. I don't know if that is true for doubles or not. The loss of precision warning is probably related to that issue. If it were me, I would keep them as integer values since in real life it is all integers, and worry about the high resistor values later when everything else is running.

Regardless, your cCode function should take a character as an argument and return an integer and your cResist function should take three integers as arguments. Irrespective of whether you have cResist return an integer or a double, there is no reason not to pass it integers.

Narue is correct in one of the other threads that the computer's not picky about calling a character an integer and can handle that situation, but for logic's sake I think you might as well declare it what it is. None of the arguments passed to the functions should be doubles.

Ty for all your help and for showing me the other post from my peer i found out that my switch function was off

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.