C++ example with input arguments using flags


/ Published in: C++
Save to your folder(s)

asdfasdfasdfasd


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Contact {
  9. public:
  10. Contact() :
  11. m_firstname(""),
  12. m_lastname(""),
  13. m_email(""),
  14. m_phone(""),
  15. m_debug_mode(false),
  16. m_cnt(0),
  17. m_already_saved(false)
  18. {};
  19. ~Contact()
  20. {};
  21.  
  22. void displayHelp() {
  23. cout << " Welcome to new_user. USAGE:" << endl;
  24. cout << "-d, --debug Enable debug mode" << endl;
  25. cout << "-e, --email <email-address> Email of the user" << endl;
  26. cout << "-h, --help Print this help" << endl;
  27. cout << "-n, --name <first-name> First name of the user" << endl;
  28. cout << "-p, --phone <phone-number> Phone number of the user" << endl;
  29. cout << "--show Show addresslist" << endl;
  30. cout << "-s, --surname <last-name> Last name of the user" << endl;
  31. };
  32.  
  33. bool welcomeScreen(int argc, char* argv[]) {
  34. int step = 2;
  35. if (argc == 1) return false;
  36. else {
  37. for (int i = 1; i < argc; i = i + step) {
  38. if (!strcmp(argv[i],"-n") || !strcmp(argv[i],"--name")){
  39. m_firstname = argv[i+1];
  40. step = 2;
  41.  
  42. } else if (!strcmp(argv[i],"-s") || !strcmp(argv[i],"--surname")){
  43. m_lastname = argv[i+1];
  44. step = 2;
  45.  
  46. } else if (!strcmp(argv[i],"-e") || !strcmp(argv[i],"--email")) {
  47. m_email = argv[i+1];
  48. step = 2;
  49.  
  50. } else if (!strcmp(argv[i],"-p") || !strcmp(argv[i],"--phone")) {
  51. m_phone = argv[i+1];
  52. step = 2;
  53.  
  54. } else if (!strcmp(argv[i],"-d") || !strcmp(argv[i],"--debug")) {
  55. m_debug_mode = true;
  56. step = 1;
  57. } else if (!strcmp(argv[i],"--show")) {
  58. readAddressList();
  59.  
  60. } else if (!strcmp(argv[i],"-h") || !strcmp(argv[i],"--help")) {
  61. displayHelp();
  62. step = 1;
  63.  
  64. } else {
  65. cout << "[UNKNOWN OPTION " << argv[i] << "]" << endl;
  66. displayHelp();
  67. step = 1;
  68. return false;
  69. }
  70. } //end for-loop
  71. if (m_firstname != "" || m_lastname != "" || m_email != "" || m_phone != "") m_cnt++;
  72. } //end if-else-block
  73. return true;
  74. };
  75.  
  76. int printWelcomeHeader() {
  77. int option;
  78. cout << "********************************************" << endl;
  79. cout << "*********WELCOME TO THE ADDRESSLIST*********" << endl;
  80. cout << "********************************************" << endl;
  81. if (m_debug_mode == true) cout << "[DEBUG] mode is on" << endl;
  82. if (m_cnt != 0) cout << m_cnt << " contact(s) ready to be saved. Select option 2" << endl;
  83. cout << "----------OPTIONS----------" << endl;
  84. cout << ". 1. Show contacts ." << endl;
  85. cout << ". 2. Save contact to file ." << endl;
  86. cout << ". 3. Exit ." << endl;
  87. cout << "---------------------------" << endl;
  88. cout << "Option: "; cin >> option;
  89. cout << "---------" << endl;
  90. return option;
  91. };
  92.  
  93. void readAddressList() {
  94. string line;
  95. ifstream addresslist_in("addresslist.txt");
  96. if (m_debug_mode) cout << "[DEBUG]Reading from file..." << endl;
  97. if (addresslist_in.is_open()) {
  98. cout << " @@@@@@@@@@@@@@@@@@@@@[addresslist.txt]@@@@@@@@@@@@@@@@@@@@@" << endl;
  99. while ( getline (addresslist_in, line) ) {
  100. cout << " " << line << "\n";
  101. }
  102. addresslist_in.close();
  103. cout << " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
  104. if (m_debug_mode) {
  105. cout << "[DEBUG]File was read successfully..." << endl;
  106. }
  107. } else cout << "[ERROR]Unable to open file." << endl;
  108. };
  109.  
  110. void writeToAddressList() {
  111. ofstream addresslist_out;
  112. if (m_debug_mode) cout << "[DEBUG]Saving contact to addresslist.txt ..." << endl;
  113. if (m_firstname == "" && m_lastname == "" && m_email == "" && m_phone == "") {
  114. cout << "Cannot creaty empty contact." << endl;
  115. return;
  116. } else {
  117. if (m_already_saved == false) {
  118. if (m_already_saved == false) {
  119. addresslist_out.open("addresslist.txt", ios::out | ios::app);
  120. addresslist_out << "----------------------------------------------\n";
  121. addresslist_out << "Name: " + m_firstname + " " + m_lastname + "\n";
  122. addresslist_out << "Email: " + m_email + "\n";
  123. addresslist_out << "Phone number: " + m_phone + "\n";
  124. addresslist_out.close();
  125. if (m_debug_mode) {
  126. cout << "[DEBUG]Contact successfully saved!!" << endl;
  127. if (m_cnt > 0) m_cnt--;
  128. }
  129. m_already_saved = true;
  130. }
  131. } else cout << "Contact already saved!!" << endl;
  132. }
  133. };
  134.  
  135. public:
  136. string m_firstname;
  137. string m_lastname;
  138. string m_email;
  139. string m_phone;
  140. bool m_debug_mode;
  141. bool m_already_saved;
  142. int m_cnt;
  143. };
  144.  
  145.  
  146. int main(int argc, char* argv[]) {
  147.  
  148. Contact* contact = new Contact();
  149. int opt;
  150. string name, surname, email, phone,line;
  151. bool cont = true;
  152.  
  153. if (!contact->welcomeScreen(argc,argv)) {
  154. cout << "(You can add the -h or --help flag to display help)" << endl;
  155. cout << "Exiting application.." << endl;
  156. return 0;
  157. } else { //input arguments
  158. if (argc > 2) cout << "New contact already created from command line!" << endl;
  159. }
  160.  
  161. while (cont ==true) {
  162. opt = contact->printWelcomeHeader();
  163. switch (opt) {
  164. case 1:
  165. {
  166. contact->readAddressList();
  167. }
  168. break;
  169. case 2:
  170. {
  171. contact->writeToAddressList();
  172. break;
  173. }
  174. case 3:
  175. {
  176. cont = false;
  177. cout << "Exiting application..." << endl;
  178. break;
  179. }
  180. default:
  181. cout << "Wrong option entered." << endl;
  182. break;
  183. } //end of switch
  184.  
  185. } //end of while
  186.  
  187. return 0;
  188. }

URL: dfasdfasdfasd

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.