How To Create Your Own Command-Line User Interface

I wrote a program last semester that calculates complex arithmetic using Fortran, and with that program I also started dabbling in CLI design. I set out today to write a program that does nothing more than present a testbed for a command-line interface. You should be able to copy this code and apply it to any project as a template, substituting your own commands and conditions.

~Jonathan

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
/*
 *	Programmer:  Jonathan Landrum
 *	Date:        29 February 2012
 *
 *	Program:     userInterface.cpp
 *	Purpose:     To provide a testbed for
 *                   developing a useful CLI.
 *	Assumptions: None.
 */
 
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
// --------------------------------------------------------
// main():
// --------------------------------------------------------
int main () {
 
	// Variables
	char   response;	// User response to the UI
	string input;		// User input to return
	string buffer;		// Disgarded carriage returns
	int    random;		// Random number for random phrase
 
	// Introduce the program
	cout << endl;
	cout << "-------------------------------" << endl;
	cout << "-    Hello World Generator    -" << endl;
	cout << "-------------------------------" << endl;
	cout << endl;
	cout << endl;
	cout << "ENTER A COMMAND:" << endl;
	cout << endl;
	cout << "     Echo a phrase........[E]" << endl;
	cout << "     Random Message.......[R]" << endl;
	cout << endl;
	cout << "     This help document...[?]" << endl;
	cout << "     EXIT.................[X]" << endl;
	cout << endl;
	cin  >> response;
	cout << endl;
 
 
	// Ensure we have a valid command
	while (response != 'e' && response != 'E' &&
	       response != 'r' && response != 'R' &&
	       response != 'x' && response != 'X' &&
	       response != '?') {
		cout << endl;
		cout << "Error:  << UNKNOWN COMMAND >>" << endl;
		cout << endl;
		cout << endl;
		cout << "ENTER A COMMAND: ";
		cin  >> response;
		cout << endl;
	} // End while
 
 
	// Keep the user in the loop unless they type "X"
	while (response != 'x' || response != 'X') {
		if (response == 'e' || response == 'E') {
			// Get rid of extraneous carriage returns
			getline(cin,buffer);
			cout << "Enter a string: ";
			getline(cin,input);
			cout << endl;
			cout << input << endl << endl;
		} else if (response == 'r' || response == 'R') {
			// Gives us a "random" seed from system time
			srand  (time(NULL));
			// Gives us a natural number from 1 to 5
			random = (rand() % 5) + 1;
			cout << "How to say hello in..." << endl;
			switch (random) {
				case 1:
					cout << "привет (Russian)" << endl;
					break;
				case 2:
					cout << "Hola (Spanish)" << endl;
					break;
				case 3:
					cout << "Hallo (German)" << endl;
					break;
				case 4:
					cout << "Bonjour (French)" << endl;
					break;
				case 5:
					cout << "Hello (English)" << endl;
					break;
				default:
					break;
			} // End random hello switch
			cout << endl;
		} else if (response == '?') {
			cout << "     Echo a phrase........[E]" << endl;
			cout << "     Random Message.......[R]" << endl;
			cout << endl;
			cout << "     This help document...[?]" << endl;
			cout << "     EXIT.................[X]" << endl;
			cout << endl;
		} else {
			break;
		} // End if
 
		cout << "ENTER A COMMAND: ";
		cin  >> response;
		cout << endl;
		while (response != 'e' && response != 'E' &&
		       response != 'r' && response != 'R' &&
		       response != 'x' && response != 'X' &&
		       response != '?') {
			cout << endl;
			cout << "Error:  << UNKNOWN COMMAND >>" << endl;
			cout << endl;
			cout << endl;
			cout << "ENTER A COMMAND: ";
			cin  >> response;
			cout << endl;
		} // End while
	} // End while
 
	// Exit
	cout << "\\\\//_ Live long and prosper." << endl;
	return (0);
} // End main

Jonathan Landrum
Jonathan Landrum is a full-time husband and student, and a part-time research assistant and IT guy. He both works and studies at Mississippi College, where he is pursuing a Bachelor of Science in Computer Science.

Name:
Email:
Message:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">