Menu: Difference between revisions

Content added Content deleted
m (→‎{{header|C++}}: formatting)
Line 98: Line 98:


=={{header|C++}}==
=={{header|C++}}==
<lang C++>#include <iostream>
<lang cpp>#include <iostream>
#include <boost/regex.hpp>
#include <boost/regex.hpp>
#include <cstdlib>
#include <cstdlib>
#include <string>
#include <string>
using namespace std ;
using namespace std;


void printMenu( const string * , int ) ; //prints menu
void printMenu(const string *, int); //prints menu
bool checkEntry( string , const string * , int ) ; //checks whether entered data is in required range
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 *, int); //function that performs it all


void dataEntry ( const string *terms , int size ) {
void dataEntry(const string *terms, int size) {
if ( size == 0 ) { //we return an empty string when we call the function with an empty list
if (size == 0) { //we return an empty string when we call the function with an empty list
cout << '\n' ;
cout << '\n';
return ;
return;
}
}
printMenu ( terms , size ) ;
printMenu(terms, size);
cout << "Enter a number from 1 to " << size << " :\n" ;
cout << "Enter a number from 1 to " << size << " :\n";

string entry ;
cin >> entry ;
string entry;
bool ok = checkEntry( entry , terms , size ) ;
cin >> entry;

while ( ! ok ) {
printMenu( terms , size ) ;
bool ok = checkEntry(entry, terms, size);
while (!ok) {
cout << "Enter a number from 1 to " << size << " :\n" ;
cin >> entry ;
printMenu(terms, size);
ok = checkEntry ( entry , 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' ;
int number = atoi(entry.c_str());
cout << terms[number - 1] << '\n' ;
}
}


void printMenu ( const string *terms , int num ) {
void printMenu(const string *terms, int num) {
for ( int i = 1 ; i < num + 1 ; i++ ) {
for (int i = 1 ; i < num + 1 ; i++) {
cout << i << ')' << terms[ i - 1 ] << '\n' ;
cout << i << ')' << terms[ i - 1 ] << '\n';
}
}
}
}


bool checkEntry( string myEntry , const string *terms , int num ) {
bool checkEntry(string myEntry, const string *terms, int num) {
boost::regex e ( "^\\d+$" ) ;
boost::regex e("^\\d+$");
if ( ! ( boost::regex_match( myEntry , e ) ) )
if (!boost::regex_match(myEntry, e))
return false ;
return false;
int number = atoi( myEntry.c_str( ) ) ;
int number = atoi(myEntry.c_str());
if ( number < 1 || number > num )
if (number < 1 || number > num)
return false ;
return false;
return true ;
return true;
}
}


int main( ) {
int main( ) {
const string terms[ ] = { "fee fie" , "huff and puff" , "mirror mirror" , "tick tock" } ;
const string terms[ ] = { "fee fie" , "huff and puff" , "mirror mirror" , "tick tock" };
int size = sizeof terms / sizeof *terms ;
int size = sizeof terms / sizeof *terms;
dataEntry( terms , size ) ;
dataEntry(terms, size);
return 0 ;
return 0;
}</lang>
}
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==