Infinite loop by input validation

I tested out my program and when I input a value less than 3 and higher than 20 it gives me the error message intended but afterwards when I type in a character or word it just causes an infinite loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//Draw a single line of fiver uppercase Xs using two kinds of count-controlled loops
#include <iostream>
using namespace std;

int main()
{

    //Variables declared
    int count1 = 1, limit = 0, count2 = 1;
    char doAgain;

    cout << "**************** Drawing Squares Program ******************************" << endl;
    cout << "* Algorithm generates a hollow square, using the character +, - and | *" << endl;
    cout << "* Acceptable size dimension: Any value from 3 to 20. Choose carefully.*" << endl;
    cout << "***********************************************************************" << endl;

do{
      cout << "Side Size: ";
      cin >> limit;

    count1 = 1;
    cout<< '+';



    while(!cin || limit<0)
    {
        cin.clear();
        cin.ignore(10000,'\n');
        cout << "OOPS! Looks like you typed some bad data here!" << endl;
        cout << "The acceptable dimension should range from 3 to 20, so choose carefully..." << endl;
        cout << "Side Size: ";
        cin >> limit;

        count1 = 1;
        cout<< '+';
    }

    while(limit < 3 || limit > 20)
    {
        cout << "OOPS! Looks like you typed some bad data here!" << endl;
        cout << "The acceptable dimension should range from 3 to 20, so choose carefully..." << endl;
        cout << "Side Size: ";
        cin >> limit;

        count1 = 1;
        cout<< '+';
    }


    while(count1 <= limit-2)
    {
        cout << "-";
        count1++; //Loop update condition
    }
        cout<< '+';
        cout << endl;

    for(int i = 0; i <= count1; i++)
        {
            cout<< '|';
            count1 = 1;
    while(count1 <= limit-2)
    {
        cout << " ";
        count1++; //Loop update condition
    }
        cout << '|' << endl;
        }

        count1 = 1;
        cout<< '+';

    while(count1 <= limit-2)
    {
        cout << "-";
        count1++; //Loop update condition
    }
        cout<< '+';

    cout<< endl;

    cout <<"To try my shape generator program again type Y for Yes and N for No: ";
    cin >> doAgain;

    if (doAgain == 'N' || doAgain == 'n')
    {
       cout << "Now exiting the shape generator program......." << endl;
       /*return 0; */ //Remainder of loop problem

    }
}while(doAgain == 'Y' || doAgain == 'y');

    return 0;
}


any help is appreciated.
Separate your input and your drawing by using functions. Otherwise your code structure becomes incomprehensible and people will be reluctant to sort it out for you.

Where you have incorrect input you will have to tidy up the stream afterwards; e.g. by using cin.clear and cin.ignore.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace std;

string operator *( int n, string s ) { return n ? s + ( n - 1 ) * s : ""; }


//======================================================================


int getInt( string prompt, int mn, int mx )
{
   int value;
   cout << prompt;

   if ( cin >> value && value >= mn && value <= mx )    // if OK
   {
       return value;
   }
   else                                                 // if not OK
   {
       cin.clear();   cin.ignore( 1000, '\n' );         // tidy up the mess
       return getInt( prompt, mn, mx );                 // try again
   }
}


//======================================================================


void drawRectangle( int width, int height )
{
   string top = '+' + string( width - 2, '-' ) + "+\n";
   string mid = '|' + string( width - 2, ' ' ) + "|\n";
   cout << top + ( height - 2 ) * mid + top;
}


//======================================================================


int main()
{
   int width  = getInt( "Enter width  (3<=w<=20 ): ", 3, 20 );
   int height = getInt( "Enter height (3<=h<=20 ): ", 3, 20 );
   drawRectangle( width, height );
}

Enter width  (3<=w<=20 ): hello
Enter width  (3<=w<=20 ): 2
Enter width  (3<=w<=20 ): 32
Enter width  (3<=w<=20 ): 6
Enter height (3<=h<=20 ): Go away
Enter height (3<=h<=20 ): 4
+----+
|    |
|    |
+----+
Last edited on
Topic archived. No new replies allowed.