Trying to create a pointer to an array (string of characters) that the user enters.
Here i define my pointer and array in my class:
private:
char userWord[200]; // input word
char * userWordPtr; // Pointer to the beginning of the input word
Here is the function that reads in the information:
void WordFont::inputWord()
{
cout << "What word would you like to draw?\n"
<< "The useable characters are: A E I O U Y B C D L M N R S T\n" ;
cout << "Please enter your word: ";
cin.get(userWord, 200);
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
userWordPtr = &userWord;
}
The user enters a word and my code does something (long if-else ladder) based on the letters of the word. But in passing a * Ptr to the if-else ladder, I can’t get the if-else ladder to recognize the null (/0) character when I’m incrementing the position of the pointer. I also get an error using the ‘&’ to reference the address of the beginning of the char array. I can’t figure out why I’m getting that error, and why the if-else ladder below doesn’t exit til ‘i’ gets to whatever random location in memory has a ‘/0’ character. If I do something as simple as entering ‘AAA’, it will well past the 4th position where the ‘/0’ character is.
Here’s the loop:
void WordFont::buildOutputArray(char * Ptr)
{
length = 0;
bool switchLooper = true;
int i = 0;
do
{
if(*Ptr + i == 'a' || *Ptr + i == 'A')
constructA();
else if (*Ptr + i == 'e' || *Ptr + i == 'E')
constructE();
else if (*Ptr + i == 'i' || *Ptr + i == 'I')
constructI();
else if (*Ptr + i == 'o' || *Ptr + i == 'O')
constructO();
else if(*Ptr + i == 'u' || *Ptr + i == 'U')
constructU();
else if(*Ptr + i == 'b' || *Ptr + i == 'B')
constructB();
else if(*Ptr + i == 'c' || *Ptr + i == 'C')
constructC();
else if(*Ptr + i == 'd' || *Ptr + i == 'D')
constructD();
else if(*Ptr + i == 'l' || *Ptr + i == 'L')
constructL();
else if(*Ptr + i == 'm' || *Ptr + i == 'M')
constructM();
else if(*Ptr + i == 'n' || *Ptr + i == 'N')
constructN();
else if(*Ptr + i == 'r' || *Ptr + i == 'R')
constructR();
else if(*Ptr + i == 's' || *Ptr + i == 'S')
constructS();
else if(*Ptr + i == 't' || *Ptr + i == 'T')
constructT();
else if(*Ptr + i == '\0')
switchLooper = false;
i++;
}while(switchLooper);
}
Its probably a easy newbie mistake, and I’m sure you’ll spend notice lots of other problems, but any help I greatly appreciate.
Thanks,
Tom
Actually, what you are doing by using
*Ptr + iis:Ptrpointer (you are accessing the value at the address pointed byPtr)ito the value contained at the addressPtrSerge gave you the answer, you have to use
*(Ptr + i)to get the character at that address.Here, you are shifting the
Ptrpointer ofi“blocks”.An array of char (or whatever) is “like” a pointer (there are a bunch of differences but I let you the pleasure of discovering them ), so when you are using
str[i]it’s quite the same thing than*(str + i).You shouldn’t have this problem anymore.
What if the user enters 200 characters?