Menu: Difference between revisions

1,670 bytes added ,  14 years ago
no edit summary
(Added a solution for Haskell)
No edit summary
Line 96:
return EXIT_SUCCESS;
}</lang>
 
=={{header|C++}}==
<lang C++>#include <iostream>
#include <boost/regex.hpp>
#include <cstdlib>
#include <string>
using namespace std ;
 
void printMenu( const string * , int ) ; //prints menu
bool checkEntry( string , const string * , int ) ; //checks whether entered data is in required range
void dataEntry( const string * , int ) ; //function that performs it all
 
void dataEntry ( const string *terms , int size ) {
if ( size == 0 ) { //we return an empty string when we call the function with an empty list
cout << '\n' ;
return ;
}
printMenu ( terms , size ) ;
cout << "Enter a number from 1 to " << size << " :\n" ;
string entry ;
cin >> entry ;
bool ok = checkEntry( entry , terms , size ) ;
while ( ! ok ) {
printMenu( terms , size ) ;
cout << "Enter a number from 1 to " << size << " :\n" ;
cin >> entry ;
ok = checkEntry ( entry , terms , size ) ;
}
int number = atoi( entry.c_str( ) ) ;
cout << *( terms + number - 1 ) << '\n' ;
}
 
void printMenu ( const string *terms , int num ) {
for ( int i = 1 ; i < num + 1 ; i++ ) {
cout << i << ')' << terms[ i - 1 ] << '\n' ;
}
}
 
bool checkEntry( string myEntry , const string *terms , int num ) {
boost::regex e ( "^\\d+$" ) ;
if ( ! ( boost::regex_match( myEntry , e ) ) )
return false ;
int number = atoi( myEntry.c_str( ) ) ;
if ( number < 1 || number > num )
return false ;
return true ;
}
 
int main( ) {
const string terms[ ] = { "fee fie" , "huff and puff" , "mirror mirror" , "tick tock" } ;
int size = sizeof terms / sizeof *terms ;
dataEntry( terms , size ) ;
return 0 ;
}
</lang>
 
=={{header|Common Lisp}}==
262

edits