Break two loops with a error

This is a discussion on Break two loops with a error within the C++ Programming forums, part of the General Programming Boards category; Hey Everybody, I wrote this code: Code: #include <windows.h> #include <iostream> using std::cout; using std::cin; using std::endl; using std::fixed; int ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Break two loops with a error

    Hey Everybody,

    I wrote this code:
    Code:
    #include <windows.h>
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    
    int main()
    {
        int a, line, col, ord;
        
        HANDLE hOut;
        COORD Position;
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
            cout << "What the number of line and column?";
            cin >> ord;
        
        for(line=0; line<=ord-1; line++){
                     Position.Y = line+2;
                     
                 for(col = 0; col<=ord-1; col++){
                            Position.X = 3+(col*8);
                            SetConsoleCursorPosition(hOut, Position);
                            
                           cout << "a" << col <<":";
                           if( ! ( cin >> a ) ){
                               cout << "It's not a number";
                               }
                           
                           if (col == ord){ cout << endl; }
                           
                           
                           }
                 }
    
        
        system("pause");
        return 0;
    }
    So the bold line, the program tests the variable a.
    How can I finish the program in this line? If I use exit(1) or Return 0, The window close immediately... How can I finish the program with a error mensage, and restart the program?

    Thanks, have a good night.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Well, first you're not checking the validity of ord, so to be consistent you probably want to do that as well. How about wrapping that functionality into a function (promptForInput()?) and throwing an exception if it's invalid?

    Code:
    try {
        promptForInput();
    } catch (InvalidInputError & err) {
        // log message here
    }

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Cassady View Post
    Hey Everybody,

    I wrote this code:
    Code:
    #include <windows.h>
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    
    int main()
    {
        int a, line, col, ord;
        
        HANDLE hOut;
        COORD Position;
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
            cout << "What the number of line and column?";
            cin >> ord;
        
        for(line=0; line<=ord-1; line++){
                     Position.Y = line+2;
                     
                 for(col = 0; col<=ord-1; col++){
                            Position.X = 3+(col*8);
                            SetConsoleCursorPosition(hOut, Position);
                            
                           cout << "a" << col <<":";
                           if( ! ( cin >> a ) ){
                               cout << "It's not a number";
                                getch();
                                exit(1);
                               }
                           
                           if (col == ord){ cout << endl; }
                           
                           
                           }
                 }
    
        
        system("pause");
        return 0;
    }
    Thanks, have a good night.
    I think this might do well.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I wouldn't use exit to break two loops. That is overkill and makes adding to or modifying the program much harder.

    I very much like the idea of using a separate function for nested loops, although that might be a bit more advanced than the OP is ready for.

    One simple solution is to use a new bool variable as a flag. You could call it input_valid and initialize it to true. If the input fails set it to false and break the loop, then check it each time through the outer loop and break that loop if it is false.

    However, there is yet another option that I like best in this case. Just ask for input again immediately if bad input is given. If you want to "restart" the program, you'd have to clear the error in cin and clear the bad input. This example does it immediately and asks the user to try again. Replace the bold code in the original post with:
    Code:
                           while( ! ( cin >> a ) ){
                               cin.clear(); // clear the error in cin
                               cin.ignore(1000, '\n'); // ignore the bad input
                               cout << "It's not a number, try again: "; // re-prompt
                               }

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Thanks Guys, for the replys!

    I need to restart the program when the user enter a incorrect value.
    So, I think to make a function for this, when the user enter a incorrect value the function call system("cls"), and clear the prompt and then the program call the function again...

    Code:
    void Make(int ord){
                     int a, line, col;
                     int matriz[5][5];
                     
                     HANDLE hOut;
                     COORD Position;
    
                     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
                     
                     for(line=0; line<=ord-1; line++){
                     Position.Y = line+2;
                     
                 for(col = 0; col<=ord-1; col++){
                            Position.X = 3+(col*col);
                            SetConsoleCursorPosition(hOut, Position);
                            
                           cout << "a" << col <<":";
                           cin >> matriz[line][col];
                           
                           if (col == ord){ cout << endl; }
                                      
                           
                           
                           if (col == ord){ cout << endl; }
                           
                           
                           }
                 }
          }
    Is it possible in this way?
    I couldnt get set the cursor position in this function.

    Sorry for the persistence, and thanks again.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    Please indent your code properly and consistently, e.g.,
    Code:
    void Make(int ord){
        int a, line, col;
        int matriz[5][5];
    
        HANDLE hOut;
        COORD Position;
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
        for(line=0; line<=ord-1; line++){
            Position.Y = line+2;
    
            for(col = 0; col<=ord-1; col++){
                Position.X = 3+(col*col);
                SetConsoleCursorPosition(hOut, Position);
    
                cout << "a" << col <<":";
                cin >> matriz[line][col];
    
                if (col == ord){
                    cout << endl;
                }
                if (col == ord){
                    cout << endl;
                }
            }
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Quote Originally Posted by Cassady View Post

    I couldnt get set the cursor position in this function.
    I corrected it.

    Maybe, A easier way for make this it is when the user enter with a incorrect value, the program automatically clear the incorrect value, and the user try again.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> A easier way for make this it is when the user enter with a incorrect value, the program
    >> automatically clear the incorrect value, and the user try again.

    Yep, see my post above. You need to call clear() and ignore() to clear the error state and ignore the bad characters either way, so use my example to solve your problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

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