Menu: Difference between revisions
Content added Content deleted
m (→{{header|C++}}: formatting) |
|||
Line 98: | Line 98: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang |
<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( |
void printMenu(const string *, int); //prints menu |
||
bool checkEntry( |
bool checkEntry(string, const string *, int); //checks whether entered data is in required range |
||
void dataEntry( |
void dataEntry(const string *, int); //function that performs it all |
||
void dataEntry |
void dataEntry(const string *terms, int size) { |
||
if ( |
if (size == 0) { //we return an empty string when we call the function with an empty list |
||
cout << '\n' |
cout << '\n'; |
||
return |
return; |
||
} |
} |
||
printMenu |
printMenu(terms, size); |
||
cout << "Enter a number from 1 to " << size << " :\n" |
cout << "Enter a number from 1 to " << size << " :\n"; |
||
string entry ; |
|||
string entry; |
|||
cin >> entry; |
|||
⚫ | |||
bool ok = checkEntry(entry, terms, size); |
|||
⚫ | |||
⚫ | |||
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( ) ) ; |
|||
int number = atoi(entry.c_str()); |
|||
⚫ | |||
} |
} |
||
void printMenu |
void printMenu(const string *terms, int num) { |
||
for ( |
for (int i = 1 ; i < num + 1 ; i++) { |
||
cout << i << ')' << terms[ i - 1 ] << '\n' |
cout << i << ')' << terms[ i - 1 ] << '\n'; |
||
} |
} |
||
} |
} |
||
bool checkEntry( |
bool checkEntry(string myEntry, const string *terms, int num) { |
||
boost::regex e |
boost::regex e("^\\d+$"); |
||
if ( |
if (!boost::regex_match(myEntry, e)) |
||
return false |
return false; |
||
int number = atoi( |
int number = atoi(myEntry.c_str()); |
||
if ( |
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( |
dataEntry(terms, size); |
||
return 0 |
return 0; |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |