Need help using isalpha with string coverted to char array

Hi all,

I am new to C++ and I have a two player word guessing game working well. However, I would like to be able to validate whether the word entered by player 1 is a completely alphabetic word using isalpha.

The error I am getting right now is as follows:

"error: array must be initialized with a brace-enclosed initializer
char str[100]=hiddenwordtwo;"

Can someone help with this? I have been reading all over the place, but I have no idea how to fix this. I would really appreciate it! This is the relevant portion of my code:

/* isalpha portion of code */
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;

int main ()
{
char hiddenwordtwo[100];
string hiddenword;
strcpy(hiddenwordtwo,hiddenword.c_str());
hiddenword=hiddenwordtwo;

cout << "Welcome to the 2-player word guessing game!" << endl;
cout << "\nPlayer 2 please look away from the screen"
<< "\nPlayer 1 please enter any word: " << endl;
cin >> hiddenword;//player 1's entry

int i=0;
char str[100]=hiddenwordtwo;
while (str[i])
{
if (isalpha(str[i])) printf ("character %c is alphabetic\n",str[i]);
else printf ("character %c is not alphabetic\n",str[i]);
i++;
}
return 0;
}
Last edited on
Why do you need to use a character array?

What's wrong with
1
2
3
4
5
6
7
bool isAlphabetic = true;
for (unsigned i = 0; i < hiddenword.size(); ++i)
    if (!isalpha(hiddenword[i]))
    {
        isAlphabetic = false;
        break;
    }
?
Thanks! This worked perfectly. I have implemented it in my code. Huge help,
Topic archived. No new replies allowed.