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 >>
#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;
}
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
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 : ')'
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.
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.
#include <ciso646>
#include <iostream>
#include <limits>
usingnamespace 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;
}