Menu: Difference between revisions

83 bytes removed ,  14 years ago
m
m (→‎{{header|C++}}: formatting)
Line 98:
 
=={{header|C++}}==
<lang C++cpp>#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 >>string entry ;
boolcin ok = checkEntry(>> entry , terms , size ) ;
 
while ( ! ok ) {
bool ok = printMenucheckEntry(entry, terms , size ) ;
while ( ! ok ) {
cout << "Enter a number from 1 to " << size << " :\n" ;
cin >> entryprintMenu(terms, size);
okcout =<< checkEntry"Enter (a entrynumber ,from terms1 ,to " << size )<< " :\n";
cin >> entry;
ok = checkEntry(entry, terms, size);
}
 
int number = atoi( entry.c_str( ) ) ;
cout << *( terms +int number - 1= atoi(entry.c_str()) << '\n' ;
cout << "Enter a terms[number from- 1 to "] << size << " :'\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>
}
</lang>
 
=={{header|Common Lisp}}==
Anonymous user