error with strcmp


SO I'm getting an error because of this function. Everything thing in the program worked up till I try to implement this. The error I'm getting is

error C2664: 'strcmp' : cannot convert parameter 1 from 'float [7]' to 'const char *'

It's dealing with line number 40. The purpose of this function is to input a resistor value of any size, convert it to a decimal or semi decimal value. And then check what's left to the basic 24 standrad values, and return a 1 or -1 to provoke the program to respond with "this is not a valid resistor" or "This is a stand resistor"

The basic 24 values are in the string[24][7] array, and what it's suppose to do is check to see if the data match's any of 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
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
float resistorcheck( float t )
{
	float r;
	int i = 0;
	int found = 0;
	int where;
	float string[24][7] = { "1", "1.1", "1.2", "1.3", "1.5", "1.6", "1.8", "2.0", "2.2", "2.4", 
			"2.7", "3.0", "3.3", "3.6", "3.9", "4.3", "4.7", "5.1",
			"5.6", "6.2", "6.8", "7.5", "8.2", "9.1" };

	if ( t > 1000000000 )
	{
		r = ( t / 1000000000 );
	}
	if ( t > 1000000 )
	{
		r = ( t / 1000000 );
	}
	if ( t > 1000 )
	{
		r = (t / 1000 );
	}
	if ( t > 100 )
	{
		r = ( t / 100 );
	}
	if ( t > 10 )
	{
		r = ( t / 10 );
	}
	if ( t < 10 )
	{
		r = t;
	}

	i = 0;

	while (!found && i < 24 )
	{
		if( strcmp(string[i], t) == 0)
		{
			found = 1;
		}
		else
		{
			++i;
		}

		if(found)
		{
			where = 1;
		}

		else
		{
			where = -1;
		}
	}

	return(where);
}


Any help on this would be great!!!! Thanks in advance for even taking the time to check this over. if you need to see all the code just ask.
strcmp requires
#include <cstring>

and i think you want to write char string in line7
Hello,
The problem is that you have created a 2d array and in strcmp() function you are comparing the variable t to a 1d array.

So line 40 statement should be if( strcmp(string[i][j], t) == 0)

In this case you have to take another variable j.
Hope this will help you......

For c/c++ programs you can try my blog:
http://thecrazyprogrammer.blogspot.com
So I've picked at it for a while, and for a simple piece of code it's still not working how I want it too....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double resistorcheck( float t )
{
	float r;
	int i;
	float array1[24] = { 10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 
			27, 30, 33, 36, 39, 43, 47, 51,
			56, 62, 68, 75, 82, 91 };

	while ( t >= 100 )
	{
		t = t / 10;
	}
	for( i = 24; i >= 0; i--)
	{
		printf( "*" );
		if( t = array1[i] )
		{
			return r = 1;
		}
	}
	printf( "not a standrad value: %f\n", t );
	return r = -1;
}


I changed it from a string to a simpler array. It just divides it by 10 till it's less than 100, then it's suppose to go from 0 - 24 in the array elements till t = that array element, and return 1 to continue with the program.... However for some reason it's not going through the array at all. and it just continuously returns the -1 which cause's it to loop over and over asking to re input the value..... Any idea on if I'm missing a semi colon or If I've forgotten something about my "for" loop....

Once again thanks for any help on this, it's greatly appreciated, and if you'd like to view the whole code to help solve this minor issue, please just ask.
But the major problem is that you should not make direct comparisons of float like this:

1
2
3
while ( t >= 100 ) {
		t = t / 10;
	}


Or this:

if( t == array1[i] ) //changed to the equality operator, not assignment

This will almost certainly fail, because floating point numbers are stored as binary fractions, and cannot represent every real number. Google C++ floating point comparisons , to read all about it, but be aware that not all advice given in forums is right. A correct solution involves an absolute value and a scaled epsilon value.

Also, this is a bit silly:

return r = -1;

Just do this:

return -1.0;
Still getting the the bad returns on my function.... Here the whole program that I have written at the moment. It's only the function that i'm trying to correct that's screwing up the rest of it as of yet lol.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//Resistor color code // resistor calculating issue.

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "cstring" 
#define SIZE = 24

double search(char array2[][7], char array1[], int size);
double resistorcheck( float t );

int main( void )
{
	int v;
	printf( "Please input which way you are inputting resistor values:\n\n" );
	printf( " 1 = numerical valued resistors\n 2 = Color Codes.\n" );
	scanf( "%d", &v );

	if( v == 1)
	{
		int w = -1;
		float ra, rb, series, parallel;

		while( w = -1 )
		{
			printf( "Please input first resistor value\n");
			scanf("%f", &ra);
			w = resistorcheck( ra );
		}

		w = -1;

		while( w = -1 )
		{
			printf( "Please input second resistor value\n");
			scanf("%f", &rb);
			w = resistorcheck( rb );
		}

		series = ( ra + rb );
		parallel = ( (ra * rb ) / series );

		printf( "Resistors in series are: %0.2f\n", series );
		printf( "Resistors in Parallel ar: %0.2f\n\n", parallel );
	}
	if( v == 2)
	{
		char colorcodes[10][7] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white" };
		char band1[7];
		char band2[7];
		char band3[7];
		long float resistanceV;
		int x;
		int y;
		int z;
		int q;

		printf( "Enter the color codes of the resistors starting left to right:\n\n" );
		scanf("%s", band1);

		x = search(colorcodes, band1, 10);

		if( x == -1 || x == 0 )
			{
				printf( "invalid color %s", band1 );
			}
			else
			{
				printf("Enter band2: \n" );
				scanf( "%s", band2);

				y = search(colorcodes, band2, 10);

				if( y == -1 )
				{
					printf("Invalid color %s", band2);
				}

				else 
				{
					printf("Enter band3: \n" );
					scanf( "%s", band3);

					z = search(colorcodes, band3, 10 );

					if( z == -1 )
					{
						printf( "Invalid color code %s", band3 );
					}
					
					else
					{
						resistanceV = ( 10*x+y )*( pow (10.f,z));

						printf( " The resistance value is %0.2lf \n\n", resistanceV );
					}
					
					q = resistorcheck( resistanceV );

					if ( q = 1 )
					{ 
						printf( "This is a standard resistor value\n\n" );
					}
					else
					{
						printf ( "This is not a standard resistor value\n\n" );
					}
				}
			}

	}
	main();
	return 0;
}

double search(char array2[][7], char array1[], int size)
{
	int i = 0;
	int found = 0;
	int where;
	
	while (!found && i<10)
	{
		if(strcmp(array2[ i ], array1) == 0)
		{
			found = 1;
		}
		else
		{
			++i;
		}
	}

		if(found)
		{
			where = i;
		}

		else
		{
			where = -1;
		}

	return(where);
}
double resistorcheck( float t )
{
	int i;
	float array1[24] = { 1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 
			2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1,
			5.6, 6.2, 6.8, 7.5, 8.2, 9.1 };

	while ( t >= 10 ){
		t = t / 10;
	}
	for( i = 0; i <= 24; i++){
		if( t == array1[i] ){
			return 1;
	}
	printf( "Not a standrad value: %f\n", t );
	return -1.0;
}

}


The program itself is suppose to take either two standard numerical values, or take two standard colour code values. I've only gotten it to where it's finding one colour code, however I can't check to see if it's a standard value till I get the function in lines 147 - 165 to work properly... I've been using the section in lines 13 - 46 to test it. However I may have that shagged up now assuming that the function is working right.
Confounded thing...... took me two hours to notice that in my function my for statement braces were including that dam printf( "not a standard value..... and the return -1.... LOL
Last edited on
I haven't looked over the entire thing, but your two while statements assign r to -1. This is going to cause an infinite loop. Try changing the while statements to while (r == -1).

Edit: Proper indentation can help solve a lot of issues.
Last edited on
No that's ok I found my error... on line 163 the } was suppose to be on line 161 and everything past that was suppose to be moved down one lol, so no matter what my program was gonna send by -1 unless the resistor value was 1
Did you understand what I was saying about comparing floating point numbers?

Also, you could benefit from having a ShowMenu function, a switch statement to process it, with each case clause calling a function to carry out that menu option.

And this:

1
2
3
4
5
6
while( w = -1 ) //assignment operator again - need an equality ==
		{
			printf( "Please input second resistor value\n");
			scanf("%f", &rb);
			w = resistorcheck( rb );
		}

is the same problem with floating point numbers.

Sounds to me as though resistorcheck should return an int for a C program or bool for a C++ program.

With this:

float array1[24] = { 1.0, 1.1

The values default to a type of double - if you want floats you need 1.0f, 1.1f. Easier to make it an array of double.

AND DO NOT CALL MAIN on line 113. This will result in the compiler recursively placing the main function on the stack until the stack limit is exceeded. Always use loops to accomplish what you need.

Topic archived. No new replies allowed.