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.

4
Contributors
8
Replies
10
Views
9 Years
Discussion Span
Last Post by Narue

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.

A few things. One,

``pow(int, int)``

does not exist. You are trying to use the above non-existent function in line 18 of your code above. That's the "ambiguous call to overloaded function" problem. You'll have to "typecast" at least one of the integers to a float or double. It appears by the "double" in that line that you are attempting to do that. However, if so, the word "double" should be INSIDE the pow call parentheses and needs to be surrounded by parentheses itself like this:

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

Or just hardcode 10 as a double like this:

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

Here is a link to the "pow" function: http://www.cplusplus.com/reference/clibrary/cmath/pow.html

Your "calcRval" function exists from lines 13 through 20. The compiler has no idea what to do with the lines after line 20. They are not in any function. Do you intend those lines to be in their own function or are they supposed to be part of the "calcRval" function? Right now neither is true so the compiler is confused.

The problem is that if i do it like that, it gives me this error:
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

And there is still the problem with the switch. It gives me the error of C2059 and that it is missing function header (old-style formal list?)

Regarding the loss of precision warning, yes it does give you a loss of precision sometimes. I don't know why pow(int, int) doesn't exist, but for some reason it doesn't so you have to do these workarounds. I ran the code with the typecasted double and it did give me a rounding error. With v1, v2, and v3 all equal to 2, the result was 2199 rather than 2200. I changed it to the following and it gave me the right answer:

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

No typecasting needed since adding the decimal does it for you. Also it assures that the number is rounded slightly UP rather than down, which is what you want since the computer will truncate/round DOWN when it converts a double to an integer, which it has to do later since v is an integer and

``(10*v1+v2)*pow(10, (v3 + 0.00000001))``

is a double.

Regarding the switch, again see my last post. That switch statement is floating in limbo. I see from your other post that "colCode" is a function. I imagine you want your "switch" statement in that function. Right now colCode is DECLARED as a function but never IMPLEMENTED as a function. So stick the switch statement inside that function like so:

``````int colCode (int colorCode)
{
switch(colorCode)
{
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;
}
}``````

Note that I changed the name of your switch variable from colCode to colorCode to prevent confusion since the function is called "colCode". There are still problems with the switch statement itself but this is where you want that switch statement (inside the colCode function).

Ok the code has a succesful build now. It looks like this:

``````#include <iostream>
#include <cmath>
using namespace std;
int calcRval(int,int,int);
int colCode(int);
int colorCode(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.0,v3)));
return v;
}
int colCode(int colorCode){
switch(colorCode){
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 colorCode;
}``````

I type in E for c1, R for c2, and O for c3, which is supposed to give me an answer of 62,000. But it does nothing when I type in the values for c1,c2, and c3. The program just ends itself.

Type in the end of main

``cout<<val;``

If it doesn't help use getch() from conio.h

Type in the end of main

``cout<<val;``

If it doesn't help use getch() from conio.h

I've seen a lot of threads on this site saying conio.h is non-standard so don't use it. Instead here are two links explaining how to hold a console window open long enough to see output (note: not relevant if you are running from a command line).

As for the switch statement, see some of the comments in your other thread as to how to do it. Basically it does not know what you are trying to assign 0, 1, 2, etc. to. If you are returning colorCode, assign these numbers to colorCode:

``case 'B':colorCode = 0;break;``

rather than:

``case 'B':0;break;``

Or consider using "return" inside the switch statemnt itself as others do in the other thread.

Oops. My bad. We have a type mismatch.

``````int colCode(int colorCode){
switch(colorCode){
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 colorCode;
}``````

You're comparing colorCode to a character but defining colorCode as an integer! You'll need to change the code so that if the switch is based on a character, then that is what is passed to your function.

>You're comparing colorCode to a character but defining colorCode as an integer!
Not a problem. char is an integer type and int can hold any value in the basic character set.