0

I'm coding a simple Mad Libs program for school. The code I'm posting iterates through an array searching for certain prompts. Once found it uses the prompt to ask a question and records the answer. The array that holds my answers however, is omitting the first letter of every word except for the very first variable. Here is my code and a copy of the output from the array. It's shite I know, but I'm learning.

char buffer[256];
int y = 0;
//iterates through array looking for answers
for(int i = 0;i <= 256;i++)
{
    if(storyArray[i][0] == '<' && isalpha(storyArray[i][1]))
    {
        for(int x = 0; storyArray[i][x]; x++)
        {
            switch(storyArray[i][x]){
                case '<':
                    cout << "\t";
                    x++;
                    putchar(toupper(storyArray[i][x]));
                    break;
                case '>':
                    cout << ": ";
                        cin.ignore();
                    cin.getline(buffer,256);
                    strcpy(answerArray[y],buffer);
                    y++;
                    break;
                case '_':
                    cout << " ";
                    break;
                default:
                    cout << storyArray[i][x];
                    break;

            }
        }
    }
}

Output: Arrayitem1 rrayitem2

Gus
  • 6,719
  • 6
  • 37
  • 58
JohnTheWayne
  • 47
  • 1
  • 5

1 Answers1

0

You're telling it to miss the first character. That's what this does:

cin.ignore();

Take that out and you'll be fine.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • For the OP: http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – Mike D Mar 19 '13 at 02:00