So I have the below code to check the input, I'm just having trouble because apparently flushing the input causes the tests (its an assignment) to lock up and I need to manually break out of it.

The problem is that if I remove the flushing of the input it it doesn't error out correctly.

i.e. instead of enterin rrrtrtere and it errors out once, it will error out 9 times.

This is the test data:

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

This is the whole function below. So if the person presses 'i' it goes to another function, if they press 'q' it quits the whole thing, if they press 'h0','h1',v0','v1' or 'v2' it runs another function. Anything else entered should circle back to requesting input again, this is where I'm having the problem.

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;
         }
      } 
   }
}

I have read that. I can flush the input stream but this then cancels the rest of the tests and throws the code into a loop so I need to find another way to do it.

Anyone?

I'm still having trouble with this.

Any help would be appreciated.

lines 31 and 32: >> temp= &p; rowcol= atoi(temp);

That can't possibly work because atoi() expects a null-terminated string, and p is just a single character. There is an easier way to convert a character to int -- just subtract '0':

if( isdigit(p) )
    rowCol = p - '0';

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.