1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
// myui.cpp
// a collection of useful interface functions
// for a C++ console app
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
// function prototypes
void cls();
void newl(int n = 1);
string get_s();
int get_n(string);
int get_n_range(string, int, int);
bool yesNo(string);
void waitReturn(string);
void title(string, char, bool);
void menu(const vector<string>&, char);
// clear the screen
void cls()
{
// Windows:
system("cls");
// Linux/Unix = system("clear")'
}
// print a newline/blank line to console
void newl(int n)
{
for (int i = 0; i < n; i++)
cout << endl;
}
// return a string that allows a sentence/spaces
string get_s()
{
string input;
getline(cin, input);
return input;
}
// get an integer number
int get_n(string prompt)
{
int number;
string input;
while (true)
{
cout << prompt;
input = get_s();
// converts from string to number safely
stringstream myStream(input);
if (myStream >> number)
break;
else
cout << input << " -> Invalid input" << endl;
}
return number;
}
// ask user for a number within a specified range
int get_n_range(string prompt, int low, int high)
{
int number;
string input;
while (true)
{
cout << prompt << " [" << low << "-" << high << "]: ";
input = get_s();
// converts from string to number safely
stringstream myStream(input);
if (myStream >> number)
{
if (number >= low && number <= high)
break;
}
cout << input << " -> Invalid input" << endl;
}
return number;
}
// ask user for a Y/N reply to a question
bool yesNo(string question)
{
// function returns true for yes, false for no
char reply = { 0 };
string input;
while (true)
{
cout << question << " (y/n): ";
input = get_s();
if (input.length() == 1)
{
reply = toupper(input[0]);
if (reply == 'Y' || reply == 'N')
break;
}
cout << input << " -> Invalid input." << endl;
}
return (reply == 'Y' ? true : false);
}
// print a waiting message. Wait for Enter/Return
void waitReturn(string message)
{
cout << message << " ";
get_s();
}
// print a title with underline or border
void title(string title, char ch, bool border)
{
if (!border)
{
cout << title << endl;
cout << string(title.length(), ch) << endl;
}
else
{
// with a border
cout << string(title.length() + 8, ch) << endl;
cout << string(2, ch) << " " << string(title.length(), ' ')
<< " " << string(2, ch) << endl;
cout << string(2, ch) << " " << title << " " << string(2, ch)
<< endl;
cout << string(2, ch) << " " << string(title.length(), ' ')
<< " " << string(2, ch) << endl;
cout << string(title.length() + 8, ch) << endl;
}
}
// show a menu from a list of items
void menu(const vector<string>& items, char ch)
{
int slen = items[0].length();
cout << string(slen + 8, ch) << endl;
for (unsigned int i = 0; i < items.size(); i++)
{
cout << string(2, ch) << " " << items[i] << " " << string(2, ch)
<< endl;
cout << string(slen + 8, ch) << endl;
}
}
// test functions/usage:
int main()
{
cls();
title("Here is my title", '=', false);
newl();
title("WITH A BORDER", char(29), true);
newl(1);
// create a menu from a vector of strings
vector<string> menu_items;
menu_items.push_back("1 - Edit your name ");
menu_items.push_back("2 - Verify your email");
menu_items.push_back("3 - Call your Mom ");
menu_items.push_back("4 - Exit ");
menu(menu_items, char(22));
newl();
bool lee = yesNo("Is it you Lee?");
if (lee)
cout << "Hello LEEJAM!\n";
else
cout << "Hello.\n";
int age = get_n_range("How old are you?", 0, 100);
if (age != 33)
cout << "Liar!\n";
else
cout << "You are aged " << age << ".";
int fave = get_n("Enter your favourite number: ");
cout << "Lucky number " << fave;
newl(2);
waitReturn("<Hit RETURN to continue>");
newl();
return 0;
}
|