-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hw2 - Tools Module || Intermediate C and C++
74 lines (51 loc) · 1.36 KB
/
Hw2 - Tools Module || Intermediate C and C++
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
//main.cpp
#include "tools.hpp"
int main(){
//Variables
const string menu = "\n1. Print data\n2. Add new data \nChoose -1 to exit \nPlease enter your choice (1 or 2 or -1): ";
int menuChoice = 0;
welcome("Welcome to Tools Module by Eric Garcia.");
menuChoice = chooseMenu(menu, 2);
cout << "\nYou chose " << menuChoice << endl;
bye();
return 0;
}
//-------------------------------------------------------------------------------------------------------------------------
//tools.hpp
#include <iostream>
#include <stdlib.h>
using namespace std;
//Prototypes
void welcome(string s);
void bye();
int chooseMenu(string s, int num);
//------------------------------------------------------------------------------------------------------------------------
//tools.cpp
#include "tools.hpp"
void welcome(string s){
cout << s << "\n" <<endl;
}
void bye(){
cout << "Have a nice day!" << endl;
exit(0);
}
int chooseMenu(string s, int num){
bool valid = true;
int input = 0;
while (valid == true){
cout << s;
cin >> input;
//Validation
if (input == 1){
}else if (input == 2){
}else if(input < -1 || input > num){
cout << "\nInput not accepted. Please try again\n"
<< endl;
}else if (input == -1){
valid = false;
}
cin.clear();
cin.ignore(100, '\n');
}
return input;
}