how to repeatedly ask until getting a satisfactory input

How can I repeatedly ask the user until "cin" a satisfactory input?
Case 1: until "cin" 1-100, other letters and notes are not allowed
Case 2: until "cin" positive integer, negative numbers, 0, other letters and notes are not allowed

Thank you very much.
Something in this shape:

1
2
3
4
5
6
7
int m_iNumber;
	while(!(cin>>m_iNumber) || m_iNumber<1 || m_iNumber>100)
	{
		cout<<"Change input value."<<endl;
		cin.clear();
		cin.ignore(INT_MAX, '\n');
	}


All you've got to do is modify it according to what you need.
Last edited on
I think a do-while loop should be used.
Also, I think it cannot solve the problem of not allowing the notes and letters.

1
2
3
4
5
int n;
do{
   cout << "How many... ";
   cin >> n;
}while(WHAT SHOULD I WRITE HERE?)


Thank you.
Take a look at this and see if its not something you might find usefull.
Note: you can easily change the labels and goto commands to a do while 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
while(1){
    //BFH:
    cout << " Pre-Game Menu:" << endl;
    char c[1000]; //c[1] = NULL; //debugging
    c[1] = 0;

    cout << endl;
    cout << " Enter 1 for option 1. " << endl; 
    cout << " Enter 2 for option 2. " << endl;
    cout << " Type 'return' to return to proceeding menu. " << endl;
    cout << " Type 'menu' to return to the main menu. " << endl; 
    cout << " Type 'Exit' to exit. " << endl;

    enter:
    printf("%s"," >");
    string templine;
    templine = getstring();
    assign(templine, c);
    if (stringigual("exit", c) ) exit(0);
        if (stringigual("menu", c) )
            {cout << "You are already at the main menu! " << endl;}
    if (stringigual("return", c) )
            {break;}  //use this if this is in  a while loop structure, use return; if in a function etc...
        if (c[1] == 0)
            {
                 choice = c[0] - 48; //}
                 if ((choice < 1) || (choice > 2))
                      { goto enter; } 
             }
     if(!isdigit(c[0]) ) goto enter;  menuswitch(choice);
    }//end while 


you can easily change it to fit your needs.
Still not perfect however, I've noticed upon further iterations there is some sort of leftover junk from the cin that is being inserted into my variables by mistake. so i tried using cin.ignore but ended up with the following functions:

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
bool assign (const string & s, char c[])
{
    bool successfull = false;

    //if (s[0] != NULL && s[0] != '\n')
    if (s[0] != 0 && s[0] != '\n')
        {
            successfull = true;
        }
    else return false;
    for (int i = 0; i < 1000; ++i)
        {
            //if ( (s[i] == NULL) || (s[i] == '\n') )
            c[i] = s[i];
            if ( ( s[i] == 0) || ( s[i] == '\n') )
                {
                    break;
                }
            //c[i] = s[i];
        }
    return successfull;
}

bool stringigual (const string & s, const char c[])
{

    //maybe do something for caps/lowercase
    string temp = s;
    for (int i = 0; i < temp.length(); ++i)
        {//caps thing goes here
            temp[i] = toupper(s[i]);
        }
    //if ( (c[0] == NULL) || ( s[0] == NULL) )
    if ( (c[0] == 0) || ( s[0] == 0) )
        return false;
    int i = 1;
    do {
        if ( islower(c[i]) )
            {if ( (char( c[i] + 224)) != temp[i] )
                return false;}
        else
        if (c[i] != temp[i])
            return false;
        ++i;
    }//while ( (c[i] != NULL) && (c[i] != '\n') );
       while ( (c[i] != 0) && (c[i] != '\n') );
    if (s.length() > i)
        return false;
    else return true;
}

string getstring()
{
    cin.clear();
    string local = ""; string temp = "";
    getline(cin, local);
    return local;
}
Always try to put the input statement in the while condition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
	unsigned qty;

	std::cout << "How many (1 - 100): ";
	while(!(std::cin >> qty) || qty < 1 || qty >100)
	{
	    std::cout << "\nIncorrect input please try again (1-100): ";

	    // clear error flags
	    std::cin.clear();

	    // Remove the invalid input
	    std::string junk;
	    std::getline(std::cin, junk);
	}
}
thx all.
i've got it.
Topic archived. No new replies allowed.