I'm trying to use the switch function to compute the resistance of resistors. Basically, a person types in 3 letters, each representing a color; each color has a different value. Then v throws back the resistance by going through the equation. This is what I have so far.

#include <iostream>
#include <cmath>
using namespace std;
int calcRval(int,int,int);
int c1,c2,c3,val,colCode;
int main(){
char c1,c2,c3;
	cout<<"input c1:";cin>>c1;
	cout<<"input c2:";cin>>c2;
	cout<<"input c3:";cin>>c3;
	val=calcRval(c1,c2,c3);
}
int calcRval(int c1,int c2, int c3){
int v1,v2,v3,v;
	v1=colCode(c1);
	v2=colCode(c2);
	v3=colCode(c3);
	v=(10*v1+v2)*double pow(10,v3);
		return v;
}
	switch(colCode){
		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;
}

The problems it has is that it says pow is an ambiguous call to overloaded function and that the v1,v2 and v3 terms do not evaluate to a function taking 1 arguments. I have no idea what any of this means, and i cant find it in my book. I also cannot figure out why my switch will not activate.
Please help, and keep in mind that I am a total beginner.

Recommended Answers

All 9 Replies

Ok, i fixed the problem of it taking 1 argument. However, the problem dealing with pow still wont leave. It says that it could be a double pow, or long double pow, of float pow. Once agian, no idea what that means.
Also, there is still something wrong with the switch statement, it gives me error C2059. The code looks like this now:

#include <iostream>
#include <cmath>
using namespace std;
int calcRval(int,int,int);
int colCode(int);
int c1,c2,c3,val;
int main(){
char c1,c2,c3;
	cout<<"input c1:";cin>>c1;
	cout<<"input c2:";cin>>c2;
	cout<<"input c3:";cin>>c3;
	val=calcRval(c1,c2,c3);
}
int calcRval(int c1,int c2, int c3){
int v1,v2,v3,v;
	v1=colCode(c1);
	v2=colCode(c2);
	v3=colCode(c3);
	v=((10*v1+v2)*pow(10,v3));
		return v;
}
	switch(colCode){
		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;
	}

You must call switch in some fucntion, it is an operator not a function.
E.g.

int foo(char c) {
switch(c){
		case 'B':return 0;break;
		case 'N':return 1;break;
		case 'R':return 2;break;
		case 'O':return 3;break;
		case 'Y':return 4;break;
		case 'G':return 5;break;
		case 'E':return 6;break;
		case 'V':return 7;break;
		case 'A':return 8;break;
		case 'W':return 9;break;
	}
}

Then Colcode is a function which is never defined. Look for your types of variables, there are some mismathes.

Hi,

I quickly changed the code so that it should do what you intented.

#include <iostream>
#include <cmath>
using namespace std;

int calcRval(int,int,int);

// the input to colCode() is now a char
int colCode(char);

// these variables are not needed
// int c1,c2,c3,val;

int main()
{
        char c1,c2,c3;

	cout<<"input c1:";cin>>c1;
	cout<<"input c2:";cin>>c2;
	cout<<"input c3:";cin>>c3;

	int val=calcRval(c1,c2,c3);

        //  the signature "int main()" implies that we 
        // should return an int, so let's do so

        return val;
}

int calcRval(int c1,int c2, int c3)
{
        int v1,v2,v3,v;

	v1=colCode(c1);
	v2=colCode(c2);
	v3=colCode(c3);

        // note, that there is no validation against invalid input,
        // i.e. v1 .. v3 may very well be -1 here ...

	v=((10*v1+v2)*pow(10,v3));

        return v;

} // Here ends the calcRval function

int colCode(char colCode) 
{
        // map the color code letter to a value

        switch(colCode)
        {
		case 'B': return 0;
		case 'N': return 1;
		case 'R': return 2;
		case 'O': return 3;
		case 'Y': return 4;
		case 'G': return 5;
		case 'E': return 6;
		case 'V' :return 7;
		case 'A': return 8;
		case 'W': return 9;
                default:
                // invalid color
                break;
	}  // Here ends the switch

       cerr << "Ooops, invalid color code: " << colCode << endl;

       // This means invalid color
       return -1;

}  // Here ends the colCode function

I used the re-written program you provided mitrmkar. It runs, however, it has the same problem as the program i barely came up with.
It lets you type in the color codes for c1,c2, and c3, but it doesnt return any number as the answer. It just ends the program. How do i get it to return an answer of like 62000?

Of corse it won't. You shoul not return the val in main you should use

cout<<val;

Thank you so much everyone for the help. There is still one persisting problem though...if you let c3 be a high color like A or W which are worth 8 and 9 respectively, the result is -2147483648. It works for everything else, just not when the last color is a high number.

Your integer value is probably overflowing. An integer can only hold up to 2^31 -1, which is somewhere in the neighborhood of 2 billion. It can't store anything above that. If you change everything to unsigned integers you can get double that, or around 4 billion. I don't know of an easy way to store integers over 4 billion.

Thank you so much everyone for the help. There is still one persisting problem though...if you let c3 be a high color like A or W which are worth 8 and 9 respectively, the result is -2147483648. It works for everything else, just not when the last color is a high number.

Change the code so that it works with the 'double' type instead of 'int'. A double should be able to represent the maximum value that the program produces.
I.e. change the stuff that is in bold below ...

[B]double[/B] calcRval(int,int,int);
[B]double[/B] colCode(char);

[B]double[/B] calcRval(int c1,int c2, int c3)
{
    [B]double[/B] v1,v2,v3,v;
    ...
-------------------------------------------------

[B]double[/B] colCode(char colCode)
{
    ...
-------------------------------------------------
and in the main() function ...

    [B]double[/B] val = calcRval(c1, c2, c3);
    // set the precision for output
    [B]cout.precision(14);[/B]
    cout << "Value = " << val << endl;

i suggest u to use long instead of double;
this is my code for resistance ...please check this out

#include<iostream>
#include<math.h>
using namespace std;
void compile(char,char,char);
int colour(char);
int main(){
char c1;
char c2;
char c3;
int a,b,c,d;
//giving user the choice for each colour
cout<<"please enter the following words for the colours\n";
cout<<"B -> black\tN -> brown\tR ->red\n";
cout<<"O -> orange\tY -> yellow\tG ->green\n";
cout<<"U ->blue\tV ->violet\tE ->grey\n";
cout<<"W ->white\n";
//asking user for different colours
cout<<"Entr the colours\n";
cout<<"first  : ";cin>>c1;
cout<<"second : ";cin>>c2;
cout<<"third  :";cin>>c3;
compile(c1,c2,c3);//passing the argument for fn call

return 0;
}
void compile(char c1,char c2,char c3){
//defining each integer for a colour
int a=colour(c1);
int b=colour(c2);
int c=colour(c3);
long  d;
d=(long)pow(10,c);
cout<<a<<b<<"X"<<d<<"ohms"<<endl;
}
int colour(char c){
switch(c){
case'B':
return 0;
case 'N':
return 1;
case'R':
return 2;
case'O':
return 3;
case'Y':
return 4;
case'G':
return 5;
case'U':
return 6;
case'V':
return 7;
case'E':
return 8;
case'W':
return 9;
default:
break;
}
cerr<<"invalid colour code chap"<<endl;
}
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.