Advertisement
Guest User

Untitled

a guest
May 26th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. /* solarSystemUI.cpp header file
  2. ======================================================================================================
  3. ENB241 | QUT Semester 1, 2015 | SolarArray Project
  4. ======================================================================================================
  5. This class was assigned to: <Student Name> | <ID Number>
  6.  
  7. DESCRIPTION:
  8. <Brief description of what this file does>
  9. <Interfaces with user through command line interface, etc>
  10.  
  11.  
  12.  
  13.  
  14. ASSUMPTIONS
  15. *
  16. *
  17. *
  18.  
  19.  
  20. NOTES:
  21. *
  22. *
  23. *
  24.  
  25. ======================================================================================================
  26. FILE CREATED: 09-05-2015
  27. LAST MODIFIED: 24-05-2015 by C. Odsey
  28. ======================================================================================================
  29. */
  30.  
  31.  
  32.  
  33. #include <iostream>
  34. using std::cout;
  35. using std::cin;
  36. using std::cerr;
  37. using std::endl;
  38.  
  39. #include "bank.h"
  40. #include "power.h"
  41.  
  42.  
  43. // ANSI colour codes
  44. // These strings are interpreted by the terminal as a change colour command
  45. const char* DEFAULT = "\033[0m"; // Default White text
  46.  
  47. const char* RED = "\033[31m"; // Fail colour - bad :(
  48. const char* YELLOW = "\033[33m";
  49. const char* GREEN = "\033[32m"; // Pass colour - good! :)
  50. const char* BLUE = "\033[34m";
  51. const char* CYAN = "\033[36m";
  52. const char* MAGENTA = "\033[35m";
  53.  
  54.  
  55. void StartUI();
  56. void StandardUI();
  57. void AdvancedUI();
  58.  
  59. int cin_int(string variable);
  60. double cin_double(string variable);
  61. float cin_float(string variable);
  62.  
  63.  
  64. int main(int argc, char *argv[])
  65. {
  66. int flag = 1;
  67.  
  68. cout << BLUE << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << CYAN << " SOLAR SYSTEM CALCULATOR! " << BLUE << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << DEFAULT << endl << endl;
  69. // Test code!
  70. while(flag == 1){
  71. StartUI();
  72. cout << "Program Complete, would you like to run it again? (1):Yes (2):No\n";
  73. cin >> flag;
  74. while(flag < 1 || flag > 2){
  75.  
  76. cerr << "Invalid input, please enter 1 or 2 for Yes or No respetively!\n";
  77. cin >> flag;
  78. }
  79. }
  80. cout << "Program Terminating." << endl;
  81. return 0;
  82. }
  83.  
  84. void StartUI(){
  85. cout << "Would you Like to use the (1) Standard or (2) Advanced User Interface?\n";
  86. int mode = 0;
  87. mode = cin_int("the mode");
  88. while(mode < 1 || mode > 2){
  89.  
  90. cerr << "Invalid input, please enter 1 or 2 for Standard or Advanced respetively!\n";
  91. mode = cin_int("the mode");
  92. }
  93.  
  94. if(mode == 1)
  95. StandardUI();
  96. else
  97. AdvancedUI();
  98. }
  99.  
  100. void StandardUI(){
  101. //Test Standard Functionality
  102. cout << "---Standard User Interface---\n";
  103. cout << "-----------------------------\n";
  104. cout << "You will now be prompted for data, please enter the appropriate values.\n\n";
  105.  
  106. //Declare the relevant variables
  107. int banks;
  108. double ratedpower, length, invefficiency, wireloss, tiltangle;
  109. float latitude, longitude, systemdecline, angleloss;
  110.  
  111.  
  112. cout << "Please enter the rated output of the panels: ";
  113. ratedpower = cin_double("rated output");
  114.  
  115. cout << "Please enter the efficiency of the inverter: ";
  116.  
  117. cout << "Please enter the loss due to the wire: ";
  118.  
  119. cout << "Please enter the angle of tilt on the panels: ";
  120.  
  121. cout << "Please enter the latitude of the system location: ";
  122. latitude = cin_float("latitude");
  123.  
  124. cout << "Please enter the latitude of the system location: ";
  125. latitude = cin_float("latitude");
  126.  
  127. cout << "Please enter the system's yearly decline: ";
  128.  
  129. cout << "Please enter the loss due to angle: ";
  130.  
  131.  
  132.  
  133. cout << "-------------------------------------------\n";
  134. cout << "Thank you, some data will now be processed.\n\n";
  135. //Call Bank constuctor
  136.  
  137. //Call Power constructor
  138. /*
  139. Power StandardPower(ratedoutput, systemyearlydecline, angleloss, banks);
  140.  
  141. cout << "The average yearly power your system will produce is " << StandardPower.AvgYearlyPow() << " watts per year.\n";
  142. cout << "The total power your system will produce in its lifetime is " << StandardPower.TotalLifePow() << " watts.\n";
  143. cout << "The following is a printout table of the projected power production for the life of the system\n";
  144. StandardPower.PrintTable();\
  145. */
  146. }
  147.  
  148. void AdvancedUI(){
  149. //Test Advanced Functionality
  150. cout << "---Advanced User Interface---\n";
  151. cout << "-----------------------------\n";
  152. cout << "You will now be prompted for data, please enter the appropriate values.\n\n";
  153.  
  154. //Declare relevant variables
  155.  
  156. //Display power stats for the user's system
  157. /*
  158. cout << "The average yearly power your system will produce is " << Advanced.AvgYearlyPow() << " watts per year.\n";
  159. cout << "The total power your system will produce in its lifetime is " << Advanced.TotalLifePow() << " watts.\n";
  160. cout << "The following is a printout table of the projected power production for the life of the system\n";
  161. StandardPower.PrintTable();
  162. */
  163. //Display the cost-benefit stats for the user's system
  164.  
  165. }
  166.  
  167. int cin_int(string variable)
  168. {
  169. // Validates and returns INT variable input from user
  170. // Doesn't check for overflow or underflow
  171.  
  172. int inNum = 0;
  173.  
  174. do
  175. {
  176. if (cin.fail())
  177. {
  178. cin.clear();
  179. while (cin.get() != '\n');
  180. cerr << RED << "ERROR: Invalid input for " << variable << "." << DEFAULT;
  181. cout << "Please enter a correct value for " << variable << ": ";
  182. }
  183.  
  184. cout << "Enter " << variable << ": " << endl;
  185. cin >> inNum;
  186.  
  187. } while (!cin.good());
  188.  
  189.  
  190. return inNum;
  191. }
  192.  
  193. double cin_double(string variable)
  194. {
  195. // Validates and returns DOUBLE variable input from user
  196. // Doesn't check for overflow or underflow
  197.  
  198. double value = 0;
  199. bool validInput = false;
  200.  
  201. char *endPtr = NULL;
  202. string input;
  203.  
  204.  
  205. while(!validInput)
  206. {
  207. value = 0;
  208.  
  209. cin.sync();
  210.  
  211. getline(cin, input, '\n');
  212.  
  213. value = strtod(input.c_str(), &endPtr);
  214.  
  215. // Check for characters in input string
  216. if (endPtr == input || *endPtr != '\0')
  217. {
  218. cerr << RED << "ERROR: Invalid input for " << variable << "." << DEFAULT << endl;
  219. cout << "Please enter a correct value for " << variable << ": ";
  220. validInput = false;
  221. }
  222.  
  223. // otherwise correct input
  224. else
  225. validInput = true;
  226. }
  227.  
  228. return value;
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235. float cin_float(string variable)
  236. {
  237. // Validates and returns FLOAT variable input from user
  238. // Doesn't check for overflow or underflow
  239.  
  240. float value = 0;
  241. bool validInput = false;
  242.  
  243. char *endPtr = NULL;
  244. string input;
  245.  
  246.  
  247. while(!validInput)
  248. {
  249. value = 0;
  250.  
  251. cin.sync();
  252.  
  253. getline(cin, input, '\n');
  254.  
  255. value = strtof(input.c_str(), &endPtr);
  256.  
  257. // Check for characters in input string
  258. if (endPtr == input || *endPtr != '\0')
  259. {
  260. cerr << RED << "ERROR: Invalid input for " << variable << "." << DEFAULT << endl;
  261. cout << "Please enter a correct value for " << variable << ": ";
  262. validInput = false;
  263. }
  264.  
  265. // otherwise correct input
  266. else
  267. validInput = true;
  268. }
  269.  
  270. return value;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement