Function returning unexpected value

I have a function that should check at array of letters and return the number location of that letter in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char CHARSET[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char* CHARSETptr = CHARSET;

	double CharsetIndex(Char Chr)
	{
		unsigned int result;
		for (unsigned int i = 0; i < (strlen(CHARSET) - 1); i++)
		{
			if (Chr == CHARSET[i])
			{
				result = i;
				break;
			}
			break;
		}
		return result;
	};


So I the char entered is b the int returned should be 1 but currently for example it gives me zero.

Any thoughts?
Last edited on
'b' is not the same as 'B'.
if the character is not in the array result remains uninitialised.
Excellent point. B is in the array and B is what is in CharsetIndex(B).
CHARSET is not a C-string (there is no zero byte at the end). Any attempt to pass it to strlen() makes the behavior of your program undefined.
Line 14 unconditionally exits the loop.

P.S. Identifiers in ALL_UPPERCASE are conventionally reserved for preprocessor macros.
C++:
1
2
3
4
5
6
7
8
9
#include <string>
#include <cctype>

// returns std::string::npos if chr is not in the character set
std::size_t CharsetIndex( char chr )
{
    static const std::string char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
    return char_set.find( std::toupper(chr) ) ;
}

http://coliru.stacked-crooked.com/a/24696ed1edb92328

C:
1
2
3
4
5
6
7
8
9
10
#include <string.h>
#include <ctype.h>

// returns size_t(-1) if chr is not in the character set
size_t CharsetIndex( char chr ) 
{
    static const char char_set[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
    const char* ptr = strchr( char_set, toupper(chr) ) ;
    return ptr && *ptr ? ptr - char_set : size_t(-1) ;
}
You could make your code work, but it’s prone to errors. For example:
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
#include <iostream>

int charsetIndex(char* charset, char chr, char endchar);
void waitForEnter();

int main()
{
    char charset[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                       'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
                       'U', 'V', 'W', 'X', 'Y', 'Z' };
    std::cout << "'B' was found in charset at position " 
              << charsetIndex(charset, 'B', 'Z') << '\n';
    waitForEnter();
    return 0;
}

/* charsetIndex() - searches charset for the character chr.
 * charset is supposed to be not null-terminated.
 * endchar is supposed to be the last character in charset.
 * Returns the character position inside the string or -1 if endchar is met.
 */
int charsetIndex(char* charset, char chr, char endchar)
{
    for(int i = 0; charset[i] != endchar; i++) {
        if (chr == charset[i]) { return i; }
    }
    return -1; // not found
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


If endchar is not included in charset, the loop will get on reading in not previously allocated memory.

Or:
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
#include <iostream>

int charsetIndex(char* charset, char chr, size_t dim);
void waitForEnter();

int main()
{
    char charset[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                       'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
                       'U', 'V', 'W', 'X', 'Y', 'Z' };
    std::cout << "'B' was found in charset at position " 
              << charsetIndex(charset, 'B', sizeof(charset)/sizeof(char))
              << '\n';
    waitForEnter();
    return 0;
}

/* charsetIndex() - searches charset for the character chr.
 * charset is supposed to be not null-terminated.
 * dim is supposed to be charset dimension.
 * Returns the character position inside the string or -1 if not found.
 */
int charsetIndex(char* charset, char chr, size_t dim)
{
    for(size_t i = 0; i < dim; i++) {
        if (chr == charset[i]) { return i; }
    }
    return -1; // not found
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


You can get the sizeof of an array, but the sizeof of a pointer in this case would be pointless.
Topic archived. No new replies allowed.