The Calculator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2004
Posts: 11
Reputation: MaxC is an unknown quantity at this point 
Solved Threads: 0
MaxC MaxC is offline Offline
Newbie Poster

The Calculator

 
0
  #1
Jun 17th, 2004
Hello fellow members,
it's me again with the silly questions.

I was give a scenario to write a program for and I finished it (I think).
Although is pretty simple (first program) I had a hard time trying to assemble it.
It’s exactly as the scenario I was given ( validations, clear screen, return, etc.) apart from the page titles, headers and little visual display (for what I know), which is just something I had fun doing it.

If you run the program you are presented with two choices. Either one you choose after that you are asked to enter a number. If by mistake you enter anything else than a number the program crashes. Apart from that it works.

I was wondering maybe you know there is a way I can validate the cin choice.
I hope is not to confusing to read (man, I lost myself a few times in it).

Thanks anyway

  1. //---------------------------------------------------------------------
  2. // T H E C A L C U L A T O R
  3. //---------------------------------------------------------------------
  4.  
  5.  
  6. #include <iostream.h>
  7. #include <iomanip.h> // contains the declarations for the data manipulators.
  8. #include <stdlib.h> // system("cls"); used for clearing the screen.
  9.  
  10.  
  11. void HD1(); // program front main page heading
  12. int ProcessLoop(); // processing loop.
  13.  
  14. //---------------- MULTIPLICATION FUNCTION PROTOTYPE ---------------------
  15. void HD_M(); // multiplication table heading
  16. int Process_M(); // ask user for input
  17. float Calculation_M(); // compute and display result
  18.  
  19. //---------------- CALCULATION FUNCTION PROTOTYPE -------------------------
  20. void Process_C(); // Calculation Loop
  21. void Menu(); // Displays menu options.
  22. float input(); // Ask user to enter data.
  23. float Add(), Sub(); // Calculation functions.
  24. float Mul(), Div();
  25. int Mod(); // Modulos function.
  26. void LoopBack(); // return control to function.
  27.  
  28. //---------------- FRONT MAIN PAGE TITLE --------------------------------------
  29.  
  30. char PT1[] ="\n\t\t ----------------------------------------------"
  31. "\n\t\t T H E G U I L D S P R I M A R Y S C H O O L"
  32. "\n\t\t ----------------------------------------------";
  33. char PT2[] ="\n\n\n\t\t T H E C A L C U L A T O R \n\n";
  34.  
  35. //---------------- CALCULATION PAGE TITLE ----------------------------------
  36.  
  37. char HD_C1[] = "\n\t\t\tC A L C U L A T I O N\n\n";
  38. char HD_C2[] = "\t\t --> Only Numbers between 0 and 1000 <--\n\n\n";
  39.  
  40. //----------------- GLOBAL VARIABLES ---------------------------------------
  41.  
  42. char choice1; // Multiplication Table
  43. float row, column, product; // variables.
  44.  
  45.  
  46. float firstNum, secondNum, result ; // Calculator
  47. char choice; // variables.
  48.  
  49. //----------------- MAIN CONTROL -------------------------------------------
  50.  
  51. int main(){
  52. ProcessLoop();
  53. return 0;
  54. }
  55.  
  56. //----------------- PROCESSING LOOP -----------------------------------------
  57.  
  58. int ProcessLoop(){
  59. cout << PT1 << PT2 ;
  60. HD1();
  61.  
  62. while (choice1 !='C' && choice1 !='M'){
  63. cout << "\a\n\t\tERROR...Enter (M) or (C) only: ";
  64. cin >> choice1;
  65. }
  66. if (choice1 == 'M'){
  67. Process_M();
  68. }
  69. else if (choice1 == 'C'){
  70. Process_C();
  71. }
  72. return 0;
  73. }
  74.  
  75. // ---------------- PRINT PROGRAM FRONT PAGE HEADING ----------------------------------
  76.  
  77. void HD1(){
  78. cout << "\n\n\t\tTo Create a Multiplication Table --> Enter (M)"
  79. << "\n\n\n\t\t\tTo Perform a Calculation --> Enter (C)\n"
  80. << "\n\n\n\t\tEnter M or C only: ";
  81. cin >> choice1;
  82. }
  83.  
  84. //------------------------------------------------------------------------------
  85. // MULTIPLICATION PROCESS
  86. //------------------------------------------------------------------------------
  87.  
  88.  
  89. // ---------------- PRINT MULTIPLICATION TABLE FRONT PAGE ----------------------
  90.  
  91. void HD_M(){
  92. int row, column;
  93. cout << "\n M U L T I P L I C A T I O N T A B L E\n\n";
  94.  
  95. cout << " | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\n";
  96. cout << " | \n";
  97. cout << " ===============================================================\n";
  98. for (row = 1; row <=15; row++){
  99. cout << setw(7)<< row <<" |" ;
  100.  
  101. for (column = 1; column <= 20; column++){
  102. cout<<"__|";
  103. }
  104. cout << "\n";
  105. }
  106. }
  107.  
  108. //----------------- INPUT DATA --------------------------------------------------
  109.  
  110. int Process_M(){
  111. system("cls");
  112. HD_M();
  113. cout << "\n\tEnter Table Number reqired: ";
  114. cin >> row ;
  115. while (row < 0 || row > 20){
  116. cout << "\a\tERROR...Enter Table Number 1 ---> 20: ";
  117. cin >> row;
  118. }
  119. system("cls");
  120. HD_M();
  121. cout << "\n\tEnter Lines required 1 --> 15: ";
  122. cin >> column;
  123. while (column < 0 || column > 15){
  124. cout << "\a\tERROR...Enter Lines Number 1 ---> 15: ";
  125. cin >> column;
  126. }
  127. Calculation_M();
  128. return 0;
  129. }
  130.  
  131. //----------------- CALCULATE TABLE -------------------------------------------
  132.  
  133. float Calculation_M(){
  134. system("cls");
  135. char exit; // Local
  136. int i; // variables.
  137.  
  138. cout << "\n\t\t\t MULTIPLICATION TABLE OF " << row <<"\n";
  139. cout << "\t\t\t===========================\n\n";
  140. for (i = 1; i <= column; i++){
  141. cout <<"\t\t\t "<< setw(2) << i <<" __|" << "_____________ ";
  142. product = row * i;
  143. cout << "" << setw(3) << setprecision(6) << product <<"\n\t\t\t |\n";
  144. }
  145. cout <<"\n\n\t\tType \'E\' to Exit or Any other Key to continue...";
  146. cin >> exit;
  147. if (exit != 'E' && exit != 'e'){
  148. system("cls");
  149. Process_M();
  150. }
  151. else{
  152. system("cls");
  153. ProcessLoop();
  154. }
  155. return 0;
  156. }
  157.  
  158. //-------------------------------------------------------------------------------
  159. // CALCULATION PROCESS
  160. //-------------------------------------------------------------------------------
  161.  
  162.  
  163. //---------------- CALCULATION LOOP -------------------------------------------
  164.  
  165. void Process_C(){
  166. system("cls");
  167. Menu();
  168. while (choice != 'E' && choice != 'e'){
  169. switch(choice) {
  170. case '+': Add(); break;
  171. case '-': Sub(); break;
  172. case '*': Mul(); break;
  173. case '/': Div(); break;
  174. default: Mod(); break;
  175. }
  176. system("cls");
  177. Menu();
  178. }
  179. }
  180.  
  181. //--------------- DISPLAY CALCULATION MENU --------------------------------------
  182.  
  183. void Menu(){
  184. cout << HD_C1 << endl;
  185. cout << "\t\t\t (+) <-- Addition\n\n";
  186. cout << "\t\t\t (-) <-- Subtraction\n\n";
  187. cout << "\t\t\t (*) <-- Multiplication\n\n";
  188. cout << "\t\t\t (/) <-- Division\n\n";
  189. cout << "\t\t\t (%) <-- Modulos(%)\n\n\n";
  190. cout << "\t\t\t (E) <-- Exit\n\n";
  191. cout << "\n\t\t\tEnter an Operator for\n";
  192. cout << "\t\t\tthe Calculation of Your Choice: ";
  193. cin >> choice;
  194.  
  195. while (choice == 'E' || choice == 'e'){
  196. system("cls");
  197. ProcessLoop();
  198. }
  199. while (choice!='+' && choice!='-' && choice!='*' && choice!='/' && choice!='%'){
  200. cout <<"\a\t\t\tERROR...enter correct sign: ";
  201. cin >> choice;
  202. }
  203. }
  204.  
  205. //--------------- CALCULATION FUNCTIONS -------------------------------------------
  206.  
  207. float Add(){
  208. system("cls");
  209. cout << "\n\n\t\t\t\t(+)ADDITION\n\n\n";
  210. cout << HD_C2;
  211. input();
  212. result = firstNum + secondNum;
  213. cout << "\n\n\t\t\t"<<firstNum<<" + "<<secondNum<<" = "<<result <<endl;
  214. LoopBack();
  215. return result;
  216. }
  217. float Sub(){
  218. system("cls");
  219. cout << "\n\n\t\t\t\t(-)SUBTRACTION\n\n\n";
  220. cout << HD_C2;
  221. input();
  222. result = firstNum - secondNum;
  223. cout << "\n\n\t\t\t"<<firstNum<<" - "<<secondNum<<" = "<<result <<endl;
  224. LoopBack();
  225. return result;
  226. }
  227. float Mul(){
  228. system("cls");
  229. cout << "\n\n\t\t\t\t(*)MULTIPLICATION\n\n\n";
  230. cout << HD_C2;
  231. input();
  232. result = firstNum * secondNum;
  233. cout << "\n\n\t\t\t"<<firstNum<<" * "<<secondNum<<" = "<<result <<endl;
  234. LoopBack();
  235. return result;
  236. }
  237. float Div(){
  238. system("cls");
  239. cout << "\n\n\t\t\t\t(/)DIVISION\n\n\n";
  240. cout << HD_C2;
  241. input();
  242. result = firstNum / secondNum;
  243. cout << "\n\n\t\t\t"<<firstNum<<" / "<<secondNum<<" = "<<result <<endl;
  244. LoopBack();
  245. return result;
  246. }
  247. int Mod(){
  248. system("cls");
  249. cout << "\n\n\t\t\t\t(%)MODULOS\n\n\n";
  250. cout << HD_C2;
  251. cout << "\t\t\tNote: The modulos operator (%) is used\n";
  252. cout << "\t\t\tto calculate the remainder from\n";
  253. cout << "\t\t\tdividing two \"integer numbers\".\n\n";
  254. int firstNum, secondNum, result; // local variables
  255. cout << "\n\t\t\tEnter First Number: ";
  256. cin >> firstNum;
  257. cout << "\n\t\t\tEnter Second Number: ";
  258. cin >> secondNum;
  259. result = firstNum % secondNum;
  260. cout << "\n\n\t\t\t"<<firstNum<<" % "<<secondNum<<" = "<<result <<endl;
  261. LoopBack();
  262. return result;
  263. }
  264.  
  265. //-------------- INPUT DATA ----------------------------------------------------
  266.  
  267. float input(){
  268. cout << "\n\t\t\tEnter First Number: ";
  269. cin >> firstNum;
  270. cout << "\n\t\t\tEnter Second Number: ";
  271. cin >> secondNum;
  272. while ((firstNum < 0 || firstNum > 1000)||(secondNum < 0 || secondNum > 999)){
  273. cout << "\t\t\t\aERROR...numbers of digits exceed --> (1 - 1000)\n";
  274. input();
  275. }
  276. return 0;
  277. }
  278.  
  279. //-------------- RETURN CONTROL TO FUNCTION --------------------------------------
  280.  
  281. void LoopBack(){
  282. char exit; // Local variable
  283. cout <<"\n\n\n\t\tType \'E\' to Exit or Any other Key to continue...";
  284. cin >> exit;
  285. if (exit != 'E' && exit != 'e'){
  286. system("cls");
  287. Process_C();
  288. }
  289. else{
  290. system("cls");
  291. ProcessLoop();
  292. }
  293. }
MSVC++
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 173
Reputation: BinaryMayhem is an unknown quantity at this point 
Solved Threads: 9
BinaryMayhem BinaryMayhem is offline Offline
Unverified User

Re: The Calculator

 
0
  #2
Jun 17th, 2004
the problem is caused because you are trying to store a character in a float...

you should make the input a string, so you can test the data to make sure its valid.

char crows[255];

cin >> crows;

row = atof(crows);

....
while (row < 0 || row > 20){

btw....
why are you using a float. rows are always going to be an integer... it is bad programming practice to use so many glocal variables.
atof = float, atoi = integer (stlidb.h or math.h)...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC