Learning while loop

This is very helpful, but I was curious is there a way to use this piece of code in such a way
e.g. i only want the program to accept 100, 101, and 102 anything else i will give an error.

1
2
3
4
5
6
  while (!(cin >> month)) {
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "Invalid input. Try whole numbers" << endl;
	}
Last edited on
Sure there is!
No need to make the code complicated.

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

using namespace std;

int answer;

int main()
{
    cout << "Enter a number 100-102: ";
    cin >> answer;
    
    while(answer < 100 || answer > 102){
    cout << "Error occured! Try again: ";
    cin >> answer;
    }
    return 0;
}
Last edited on
lol Yeah thanks my program is a little more complicated than that..
I can only accept 100, 104 ,200, 201, 204..anything else is an Error
Last edited on
Then use the simple do while loop.

1
2
3
4
5
6
int answer = 0;
do
{
    cout << "Enter a number one of these numbers (100, 104 ,200, 201, 204): ";
    cin >> answer;
} while (answer != 100 && answer != 104 && answer != 200 && answer != 201 && answer != 204);


If you can only use the while loop then try this.
1
2
3
4
5
6
7
8
int answer = 0;
cout << "Enter one of these numbers (100, 104 ,200, 201, 204): ";
cin >> answer;
while (answer != 100 && answer != 104 && answer != 200 && answer != 201 && answer != 204)
{
    cout << "Invalid input, please enter one of these numbers (100, 104 ,200, 201, 204): ";
    cin >> answer;
}

Last edited on
Thanks,

I ended up using that style, was curious if there was any better implementation like in my first statement.
Thanks again ALL
You're welcome sir. If you have any troubles with something you can PM me via this website.
hi nameishi,

I ended up using that style, was curious if there was any better implementation like in my first statement.


There could be.
I was wondering, what about if the number to accept is more that you presently have? Will you keep using

1
2
...
while (answer != 100 && answer != 104 && answer != 200 && answer != 201 && answer != 204 && .... && ....)

??
I would rather suggest, using a function, that return a bool. Put all the valid values in an array and search if the input matches any of the value in the valid numbers which are now elements in the array. The function return false or true, then you can use a while loop to test like you are doing in your first post.

Something like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool checkNum ( const int search ) {
    bool value = false; // a bool to check
    int array[] = {100, 104, 200, 201, 204};
    int size = sizeof array / sizeof(int);

    for ( int i = 0; i < size; i++ )
        if ( array[i] == search ) {
	    value = true;
	    break; // once you have a match leave the for loop
	}

	if (!value) // if search is not true
	    cout <<
	        "Err: Invalid Input! "
		<< endl;

    return value;
}


then like your first statement:

1
2
3
4
5
6
7
8
9
10
// get a number from the user
   int number;
   string msg = "Enter one of these numbers 100, 104, 200, 201, 204: ";
   cout << msg;
   cin >> number;
   
   while ( !checkNum(number) ) {
     cout << msg;
     cin >> number;
   }

Of course, there are better way to do the searching. But for a few array numbers a for loop should do.
Last edited on
Topic archived. No new replies allowed.