Errors I need help with.

This is the code I am supposed to create using arrays:
Write a program that promots for colors Band 1, Band 2, and Band 3, and then displays the resistance in kilo-ohms. Include a helper function search that takes the three paramets- the list of strings, the size of the list, and a target string, and return sthe subscript of the lsit element that matches the target or returns -1 fi the target is not in the list. If an invalid color is entered, a message stating the the color is invalid must appear. After the resistance value appears or the invalid message appears, ask the user whether they want to decode another resistor. If no, exit the program.

I have written code but once I debug it I am getting strange errors
here is my code.


#include <stdio.h>
#include <string.h>
#include <math.h>

int search (char array2[][7], char array1[], int size);

int
main (void)

{
char COLOR_CODES [10][07] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};
char band1[7];
char band2[7];
char band3[7];
double resistance_value;
char decision='y'; // ** initialise to y
int x;
int y;
int z;

while (decision=='y') // ** test changed to y
{
printf("Enter the colors of the resistor's three bands, beginning with \n");
printf("the band nearest the end. Type the colors in lowercase letters \n");
printf("only, NO CAPS. \n");

printf("Band 1 => \n");
scanf("%s", band1);
x = search(COLOR_CODES, band1, 10);

if (x==-1)
printf("Invalid Color %s", band1);

else

printf ("Band 2 => \n");
scanf ("%s", band2);
y = search (COLOR_CODES,band2, 10);

if (y==-1)
printf("Invalide Color %s", band2);

else

printf ("Band 3 => \n");
scanf ("%s", band3);
z=search(COLOR_CODES,band3,10); // **

if (z==-1)
printf ("Invalid Color %s", band3); // **

else
resistance_value= (10*x + y)*pow(10,z);
// scanf("%lf", resistance_value); // ** removed
printf("The resistance value is '%f'\n", resistance_value); // **

printf("Would you like to decode another resistor ");
scanf_s(" %c", &decision); // ** added space after " to skip leading white space
// and & to pass address of decision to scanf

}
}

int
search (char array2[][7], char array1[], int size)

{
int i;
int found = 0;
int where;

i = 0;
while (!found && i<10)

{
printf("\n Now within while loop of search()\n"); /*use for testing purposes will remove later */
if(strcmp(array2[i],array1) == 0)
found = 1;
else
++i;
}
if(found)
where = i;
else
where = -1;

return(where);
}

and these are the errors


1>c:\users\public\documents\c++\homework9\homework9\2homework9.cpp(33): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\public\documents\c++\homework9\homework9\2homework9.cpp(42): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\public\documents\c++\homework9\homework9\2homework9.cpp(51): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\public\documents\c++\homework9\homework9\2homework9.cpp(58): error C2668: 'pow' : ambiguous call to overloaded function
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)'
1> while trying to match the argument list '(int, int)'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.67
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Many of those are warnings. Note that scanf_s is not standard and will probably not work on other compilers.

The error you get is because you pass an integer to pow and it doesn't know which version it should use (float or double). Cast the int to a double and it should work.
bUT i have resistance value to double?
What does resistance has do do with this. You are not passing resistance to pow.

Maybe you want x,y and z to be of type double?
Last edited on
Yes I changed z to double thanks
Topic archived. No new replies allowed.