So having some trouble with the error checking in the below function.

Currently if you enter something wrong like r it returns "Pleae Enter next move" which is correct. My problem is that if I enter rrr it will repeat "Please enter..." 3 times instead of just once.

I hope that makes sense. Any help would be great. Thanks,

cout<<"Please enter next move : ";
        cin>>dir;
        if( dir== 'i')
        {
           print_instructions();
           continue;
        }
        else if( dir=='q')
        {
           break;
        }
        else
        {
           char p;
           cin>>p;
           const char * temp;
           temp= &p;
           rowcol= atoi(temp);
           if((dir == 'h' && row<=1 && rowcol<=ROWS)  || (dir == 'v' && col<=2 && rowcol<=COLS)  ) 
           {
              movePuzzle(puzzle, dir,rowcol);
              print_puzzle(puzzle);
              flag = is_winning_state(puzzle);
           }
           else
           {
              continue;
           }

When the error occurs you need to flush the input buffer of all remaining characters. Narue has written an excellent article about flushing the input buffer here.

I've tried using the below before but it doesn't appear to work and I'm not sure why it didn't work. Glancing over the article you linked it looks like that is what it's telling me to do.

else
           {
              cin.clear();
              continue;
           }

cin.clear() doesn't flush the input stream -- just clears some of cin's error flags which is entirely a different animal. Re-read the article, don't just glance over it or you will miss it.

So I got it to work flusing the input... The only problem is that when it is tested from a .dat file it locks up and has to be manually broken.

So apparently I'm not allowed to flush the input. Is there any other way to get this to work?

The below is the test data I am using, it is 6 seperate lines that are fed into the program through a .dat file.

r4789kfdour49frkbjfds-r4tnjfdw0-ir4kljfdrwep943rpiofdkljfd04wpgenbp['er-=iohfgf$
ioufkljfdfdskljfpiofdsnmvfpkorekl,mfrksdveiouwefds piode
h2
v3
h-1
q

void get_input(int puzzle[][COLS])
{
   char dir='\0';
   int col=COLS-1;
   int row=ROWS-1;
   int rowcol=-1;

   bool flag = false;

   while(!flag)
   {
      cout<<"Please enter next move : ";
      cin>>dir;

      if( dir== 'i')
      {
         print_instructions();
         continue;
      }
      else if( dir=='q')
      {
         break;
      }
      else
      {
         char p;
         cin>>p;
         const char * temp;

         temp= &p;
         rowcol= atoi(temp);
         if((dir=='h' && row<=1 && rowcol<=ROWS) 
            || (dir=='v' && col<=2 && rowcol<=COLS))
         {
            movePuzzle(puzzle, dir, rowcol);
            print_puzzle(puzzle);
            flag = is_winning_state(puzzle);
         }
         else
         {
            cin.ignore(1000,'\n');
            continue;
         }
      
         if(flag)
         {
            cout<<"CONGRATULATIONS - You have solved the puzzle"<<endl;
         }
      } 
   }
}
This question has already been answered. Start a new discussion instead.