Menu: Difference between revisions

323 bytes removed ,  14 years ago
→‎{{header|C++}}: corrected; simplified
(→‎{{header|C++}}: corrected; simplified)
Line 98:
 
=={{header|C++}}==
{{incorrect|C++|The <code>dataEntry</code> function should return a string, not void.}}
<lang cpp>#include <iostream>
#include <boost/regex.hpp>
Line 104 ⟶ 103:
#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
voidbool dataEntrycheckEntry(string, const string *, int); //function that performs it all
 
voidstring dataEntry(string prompt, const string *terms, int size) {
if (size == 0) { //we return an empty string when we call the function with an empty list
coutreturn << '\n'"";
return;
}
printMenu(terms, size);
cout << "Enter a number from 1 to " << size << " :\n";
 
string entry;
cindo >> entry;{
 
bool ok = checkEntry(entry, terms, size);
while (!ok) {
printMenu(terms, size);
cout << "Enter a number from 1 to " << size << " :\n"prompt;
cin >> entry;
ok = checkEntry(entry, terms, size);
}
bool ok =while( !checkEntry(entry, terms, size) );
 
int number = atoi(entry.c_str());
cout <<return terms[number - 1] << '\n' ;
}
 
void printMenu(const string *terms, int num) {
for (int i = 1 ; i < num + 1 ; i++) {
Line 137 ⟶ 131:
}
}
 
bool checkEntry(string myEntry, const string *terms, int num) {
boost::regex e("^\\d+$");
Line 147 ⟶ 141:
return true;
}
 
int main( ) {
const string terms[ ] = { "fee fie" , "huff and puff" , "mirror mirror" , "tick tock" };
int size = sizeof terms / sizeof *terms;
cout << "You chose: " << dataEntry("Which is from the three pigs: ", terms, size);
return 0;
}</lang>
Anonymous user