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
92
93
94
95
96
97
//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<< '+';
    }

    if(limit != 1)
    {
        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
You could do something like

1
2
3
4
int a;
while(!(cin >> a) && a > 3 && a < 20){
  cout << "oh no! It seems you entered invalid input, try again.\n";
}


Edit: sry fixed it
Last edited on
Wait why is this here?

1
2
3
4
5
6
7
8
9
10
    if(limit != 1)
    {
        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<< '+';
    }
I was attempting to see if it would work as a way to stop special characters but then soon came to a realization that it did not
I was wondering if maybe it could be my formatting ?
Your formatting for what? How?
Executing things in a certain way? Also i tried your suggestion and still could not get it to work :/ i've spent hours on trying to get this done and its becoming a bit frustrating [Insert crying emoji]
Could you send the revised code?
[code]
//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 a, count1 = 1, limit = 5, count2 = 1;
char doAgain;

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

count1 = 1;
cout<< '+';

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

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

count1 = 1;
cout<< '+';

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

cout<< endl;


while(!cin > a && a > 3 && a < 20)
{
cout << "oh no! It seems you entered invalid input, try again.\n";
}

/* if(limit > 20 || 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<< '+';
} */

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;
}
[code\]
Last edited on
Hm, seems it is my cin thing yeah, one sec.
Fosho
Yeah my code, just doesn’t work, sorry, I guess I answered to fast. You could use sstream to first check for a number, and then either input again or just give the number to a. Like so...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sstream>

bool isDigit(string a){
  return (int)a[0] < (int)'9' && (int)a[0] > (int)'0';
}
//...

string in;
cin >> in;
while(!isDigit(in)){
  cout << "try again";
  cin >> in;
}
stringstream(in) >> a; // temporarily make string stream so can insert number into a 
Last edited on
I'll keep on trying thank you for the attempt :)
Yw
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
#include <iostream>
#include <cstdlib>
#include <string>

bool parse_number(std::string const& s, int& d)
{
  char*       parse_end = nullptr;
  char const* s_begin   = s.data();
  char const* s_end     = s.data() + s.size();

  d = std::strtol(s_begin, &parse_end, 10);

  return (parse_end == s_end) && (s.size());
}

bool in_closed_interval(int n, int lower, int upper) 
{ return n >= lower && n <= upper; }

int main()
{
  for (std::string token; std::cin >> token;)
  {   
    if (int n; parse_number(token, n) && in_closed_interval(n, 3, 20))
      std::cout << n << '\n';
    else 
      std::cerr << "try again\n";
  }    
}

Live demo:
http://coliru.stacked-crooked.com/a/fb73c798a870ffb4
Topic archived. No new replies allowed.