Error checking help...

This is a discussion on Error checking help... within the C++ Programming forums, part of the General Programming Boards category; So I have the below code to check the input, I'm just having trouble because apparently flushing the input causes ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Error checking help...

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

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Your problem description is as clear as mud, and has virtually no relationship to your code. When you ask a question, you can't assume people know anything about what you're trying to do that you haven't told them about.

    This bit of code is definitely a problem though;
    Code:
             char p;
             cin>>p;
             const char * temp;
    
             temp= &p;
             rowcol= atoi(temp);
    atoi() assumes it receives a string, which is ended by a zero character. All you've done is provide the address of a character, which might have anything in memory after it (no guarantee of a trailing zero character anywhere). The result of this code, alone, is undefined behaviour.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I'm not quite sure what you mean is wrong sorry... It works for every other stage except the last.

    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.

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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So as mentioned your else case, starting with p, can never work without a great deal of luck. What's supposed to happen with h-1, I wonder? You'll only read the - sign, so atoi will return (perhaps) zero, or who knows what with the garbage in temp, which means that you'll make a move (!). Then the next time you'll read the 1 as your dir, the \n character as your temp, which is illegal, so ignore will come along and throw away the next line, which contains the q.....

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    So what would be a better way of approaching it?

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Anyone got any ideas on how to help me with this? Please.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Ok so I have updated my code as below...

    The issue I am having though is if I enter i4523 it will print the instructions instead of erroring out (similarly if i put in h2, v3, h-1 it prints the puzzle again instead of just asking the enter the next move...

    Code:
    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;
    
             if(isdigit(p)) rowcol = p - '0';
    
             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;
             }
          } 
       }
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21