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.