I'm writing a C++ program to use a MySQL database (which means I'm limited to the <string.h> include file because of usages in the <mysql.h> include file).

In one function, the user will enter a 9-character string that will be stored in an array (AcctNum[10], to allow for the terminating null-character). One of the things I want to do is to verify that the first two characters of that string are one of the following: B1, B2, B3, P4, or P5.

For testing purposes, I've stripped the code down to the basics, as follows:

#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

int main() {

   char AcctNum[10];

   cout << "\nEnter a 9-digit account number: ";
   cin.getline(AcctNum, 10);
   
   if (AcctNum[0] != "B" && AcctNum[0] != "P") {
      cout << "First character must be a B or a P!\n";
   }

...
}

When I compile, I get a pair of error messages relating to line 14: if (AcctNum[0] != "B" && AcctNum[0] != "P") , which say, "ISO C++ forbids comparison between pointer and integer."

I new to C++, so I am undoubtedly confused in my understanding of arrays and pointers, but I have the impression that AcctNum[0] would return the first character in the array. And I have no idea what integer is meant, as the right side of each comparison is an ASCII character.

I will appreciate any enlightenment on where my understanding falls short, and, if possible, any suggestions how I can check the validity of a portion (first two characters, in this case) of a character array.

You want to use (AcctNum[0] != 'B' && AcctNum[0] != 'P') . There's a difference between a character 'b' (which is really an integer between 0 and 255 or between -128 and 127) and the value "b" , which is really a pointer to an array with two characters, the first whose value is 'b' (i.e. 98), the second whose value is 0 (which delineates the end of the C-style string).

That did it -- Thank you!

So if I understand this correctly, the "B" was seen as a pointer, which means the "AcctNum[0]" was seen as an integer?

AcctNum[0] is a character (type char).
'b' is a character.
"b" is a string, consisting of the character 'b' and the character '\0' (NULL terminator.) The string is considered a char *, or constant char * in terms of a literal string.

The error message, in referring to your AcctNum[0] as an int, is generalizing, in terms of comparison to a pointer.

Thank you, both!

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.