Need help making input validation work

Hi, I was wondering if anyone could help me out in making any number greater than 20 and less than 3 give an error message that redirects the user to inputting another value again. I have it to work when they first input a wrong value but then it does not work. Also when i try to input a special character such as 'a', 'b', and 'c' or even a word I get stuck in 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

//Draw a rectangle depending on user input of size
#include <iostream>
using namespace std;

int main()
{

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

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

    count1 = 1;
    cout<< '+';

    if(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<< '+';
    }

    if(limit < 3)
    {
        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;

    while(doAgain == 'N')
    {
        cout << "Now exiting the shape generator program......." << endl;
        break;
    }

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

    return 0;
}



Last edited on
Line 78
1
2
3
4
5
while(doAgain == 'N')
{
        cout << "Now exiting the shape generator program......." << endl;
        break;
}

here the break keyword will break out of current while but not out of do-while loop resulting in infinite while loop.

You want this to be following instead:
1
2
3
4
5
if (doAgain == 'N' || doAgain == 'n')
{
       cout << "Now exiting the shape generator program......." << endl;
       return 0; // or break, could also work!
}


edit:
I just noticed you duplicated this thread:
http://www.cplusplus.com/forum/beginner/263937/

what is the point of making a new thread?
Last edited on
To see if there were more replies that could help me solve this problem
Topic archived. No new replies allowed.