Reputation: 35
This function checks a two-dimensional char array and goes through each word in the array, using the size, searching for a >
which will take it into a switch statement searching for cases.
I am having a problem in the switch statement, specifically with user input the first time the switch statement goes through the default
case.
void parseWord(char story[][256], int size)
{
for (int i = 0; i < size; i++)
{
char prompt[256][32];
if (story[i][0] == '<')
{
switch (story[i][1])
{
case '#':
story[i][0] = '\n';
story[i][1] = 0;
break;
case '{':
story[i][0] = ' ';
story[i][1] = '\"';
story[i][2] = 0;
break;
case '}':
story[i][0] = '\"';
story[i][1] = ' ';
story[i][2] = 0;
break;
case '[':
story[i][0] = ' ';
story[i][1] = '\'';
story[i][2] = 0;
break;
case ']':
story[i][0] = '\'';
story[i][1] = ' ';
story[i][2] = 0;
break;
default:
int p = 1;
prompt[i][0] = (char)(toupper(story[i][1]));
for (int j = 2; story[i][j] != '>'; j++)
{
if (story[i][j] == '_')
prompt[i][p] = ' ';
else
prompt[i][p] = story[i][j];
p++;
}
cout << '\t' << prompt[i] << ": ";
cin.getline(prompt[i], 32);
int z = 0;
for (int t=0; prompt[i][t] != '\0'; t++)
{
story[i][t] = prompt[i][t];
z++;
}
story[i][z] = 0;
}
}
}
return;
}
cin.getline(prompt[i], 32);
is having a problem the first time it is ran.
This is the output I currently get when I run my function, with user input in *'s:
Web site name: Verb: *run*
Plural noun: *dogs*
Plural noun: *cats*
Proper noun: *Jimmy*
Adjective: *smart*
Noun: *table*
Noun: *desk*
Boolean operator: *xor*
Noun: *shoe*
Favorite website: *google.com*
Another website: *yahoo.com*
Adjective: *cold*
Emoticon: *:P*
As you can see, the cin.getline(prompt[i], 32)
is skipped when the code is ran and right away goes to the next Prompt and asks for user input. This is not making what is entered in Verb
equal to what Web site name
should be, but is not allowing anything to be entered into Web site name
and making it empty. Verb
is still equal to, in this case "run".
Why is the switch statement seemingly skipping the cin.getline(prompt[i], 32)
statement the first time around? All the times the default
case is ran after the first seem to be working as expected and I can't see or understand why user input would not be allowed the first time the default
case is ran in the switch.
Thanks for the help!
Upvotes: 0
Views: 500
Reputation: 35
I found that in other functions in my program that I received input with cin
. Looking at Why getline skips first line? I saw that cin
and cin.getline()
don't work well together. After trying cin.ignore()
statements (which fixed this problem but created many others) I decided to exclusively get all input using cin.getline()
. All inputs work now.
Upvotes: 0