User input verification.

I was wondering how do i put a system in place to verify user input and block the incorrect input for switch's. For example:

Options are 1-5.

How do i restrict anything less then 1 greater then 5, or characters?
Also how do i ensure the default continues to force a reloop of the switch until the correct input has been entered using cin >>
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <limits>

int main(){
    bool valid = false;
    int input;
    while(!valid){
        if(std::cin>>input){//this checks whether an integer was entered
            if(input < 6 && input > 0) valid = true;//then we have to see if this integer is in range
        }else std::cin.clear();//some cleaning up
		
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');//empty input stream

        if(!valid) std::cout << "this input is not valid\n";
    }
    std::cout << input << " is between 1 and 5\n";
    std::cin.get();
    return 0;
}
could you recode that using namespace std as thats what im used to.

So in each function i declare

bool valid = false;
int input =0;


where does my switch go within that code, or does it go below that?
could you recode that using namespace std as thats what im used to.
What difference does it make?

So in each function i declare...
int input would be there anyway, bool valid could be replaced with a break on line 9, but I think using bools is more clear.

where does my switch go within that code, or does it go below that?
Yes.
Well i tried this and it doesn't work it works fine as a stand alone but as soon as i put it into my code it just gives me lots of errors about not being enough parameters and illegal use if ( etc
My menus are mostly switchs


Errors:

gdom Of Mythos Revised.cpp(10084) : warning C4003: not enough actual parameters for macro 'max'
1>.\Kingdom Of Mythos Revised.cpp(10084) : error C2589: '(' : illegal token on right side of '::'
1>.\Kingdom Of Mythos Revised.cpp(10084) : error C2143: syntax error : missing ')' before '::'
1>.\Kingdom Of Mythos Revised.cpp(10084) : error C2059: syntax error : ')'
Last edited on
This is what i was looking for



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
cout << "Please make a selection.\n\n<1 = Start Game> <2 = Exit Program>\n";
START:	
	cin >> StartProgram;
	if (cin.fail())
	{
		cin.clear();
		cin.ignore();
		cout << "Incorrect input. Please try again.\n";
		cout << "Please re-enter selection. <1-2>\n";
		goto START;
	}
	else
	{
	switch (StartProgram)
	{
	case 1:  cout << "Now entering game...\n";
			 MainMenu();
			 break;
	case 2:  cout << "\n";
			 break;
	
	default: cout << "Incorrect Selection\n";
			 cout << "Please re-enter selection. <1-2>";
		 	 goto START;
			 break;
	}
	}




If a user enters anything other then 1 or 2, it goes to default and sends them back to the top. If they enter an incorrect input type CHAR it also sends them back and clears input. and this is a good example of when a goto has a good use. Its only for the switch its not going to random parts of a program where you could get confused as to the order of things.
Last edited on
No, it is not a good example of a goto; a simple loop would do just as well. Also, your error handling is needlessly separated into two sections when one would suffice. It is a good start, and excepting your assertion on the use of unconditional jumps, you only need a few tweeks.

For input handling, some modularity is helpful. Here is a complete example to help.

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
#include <ciso646>
#include <iostream>
#include <limits>
using namespace std;

//----------------------------------------------------------------------------
// This function modularizes the concept of the main menu. It:
//  - displays the menu
//  - gets and validates input
//  - returns a valid menu choice
int main_menu()
  {
  int result = 0;
  cout << "\nMain Menu:\n";
  while (true)
    {
    // Display the menu
    cout << "1 Do something\n"
            "2 Do another thing\n"
            "3 Stop doing things\n"
            "> "
         << flush;

    // Get input.
    // Since our input method does not account for things like "4xy",
    // we'll just clear any extraneous junk from the end of the line.
    // For a more robust input method, see:
    //   http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827
    cin >> result;

    // Validate the input as (beginning with) an integer in [1,3].
    if ((!cin)
    or  (result < 1)
    or  (result > 3))
      {
      cin.clear();
      cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
      cout << "\n"
              "Hey, that's not a valid input!\n"
              "Please try again:\n";
      }

    // Return a (guaranteed) valid menu choice
    else
      {
      cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
      break;
      }
    }
  return result;
  }

//----------------------------------------------------------------------------
// The main program here is not encumbered with the menu code.
// All it need do is use the above function to get a guaranteed-valid input,
// and act on the result.
// Simple enough.
int main()
  {
  bool done = false;
  cout << "Hello there. Welcome to something simple!\n";
  while (!done)
    {
    switch (main_menu())
      {
      case 1: cout << "\n3 + 3 = " << (3 + 3) << ".\n"; break;
      case 2: cout << "\nNo.\n";                        break;
      case 3: done = true;                              break;
      }
    }
  cout << "\nGood-bye, then.\n";
  return 0;
  }

Hope this helps.
Topic archived. No new replies allowed.