converting swtich/menu to function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

converting swtich/menu to function

 
0
  #1
Oct 19th, 2008
i have this code here that i have written out and im i want to convert it using functions.
basically it is a geometry calculator that will let you calculate the area for any geometric object (ie rectangle, cirlce, triangle)

basically i have to rewrite the code and add a loop so that the uuser can calculate more than one geometric object.

here was my code that was only with switch statements:

  1.  
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. int main ()
  9. {
  10.  
  11.  
  12.  
  13. //Declaration of Variables.
  14. double circleArea, rectangleArea, triangleArea,
  15. length, width, height, base, radius;
  16. char choice;
  17. const double PI = 3.14159;
  18.  
  19.  
  20. //Display the menu.
  21.  
  22. cout << "\tGeometry Calculator" << endl << endl;
  23. cout << setw(10) << "1. Calculate the Area of a [C]ircle" << endl;
  24. cout << setw(10) << "2. Calculate the Area of a [R]ectangle" << endl;
  25. cout << setw(10) << "3. Calculate the Area of a [T]riangle" << endl;
  26. cout << setw(6) << "4. [Q]uit" << endl;
  27.  
  28. cout << endl;
  29.  
  30. //Prompt User for choice.
  31.  
  32. cout << setw(10) << "Enter your choice (1-4)" << endl;
  33. cin >> choice;
  34. cout << endl;
  35.  
  36.  
  37. //Calculate the area of a Circle.
  38.  
  39. switch (choice)
  40. {
  41.  
  42. case 'C':
  43. case 'c':
  44. case '1':
  45.  
  46. cout << "Enter the radius of the circle: ";
  47. cin >> radius;
  48.  
  49. if (radius < 0)
  50. {
  51. cout << "Number must be greater than 0. Try again" << endl;
  52. }
  53. else
  54. {
  55. circleArea = PI * pow(radius, 2);
  56. cout << "The area of the circle is: " << circleArea;
  57. }
  58.  
  59. cout << endl;
  60.  
  61. break;
  62.  
  63.  
  64. //Calculate the area of a Rectangle.
  65.  
  66. case 'R':
  67. case 'r':
  68. case '2':
  69.  
  70. cout << "Enter the length and width of the rectangle: ";
  71. cin >> length >> width;
  72.  
  73. if (length < 0 || width < 0)
  74. {
  75. cout << "Number must be greater than 0. Try again" << endl;
  76. }
  77. else
  78. {
  79. rectangleArea = length * width;
  80. cout << "The area of the rectangle is: " << rectangleArea;
  81. }
  82.  
  83. cout << endl;
  84.  
  85. break;
  86.  
  87.  
  88. //Calculate the area of a Triangle.
  89.  
  90. case 'T':
  91. case 't':
  92. case '3':
  93.  
  94. cout << "Enter the base and height of the triangle: ";
  95. cin >> base >> height;
  96.  
  97. if (base < 0 || height < 0)
  98. {
  99. cout << "Number must be greater than 0. Try again" << endl;
  100. }
  101. else
  102. {
  103. triangleArea = base * height * .5;
  104. cout << "The area of the triangle is: " << triangleArea;
  105. }
  106.  
  107. cout << endl;
  108.  
  109. break;
  110.  
  111.  
  112. //Quit the program.
  113.  
  114. case 'Q':
  115. case 'q':
  116. case '4':
  117.  
  118. cout << "End of Program" << endl;
  119. cout << "Have a Great Day!" << endl;
  120. cout << endl;
  121.  
  122. break;
  123.  
  124.  
  125. //Invalid number.
  126.  
  127. default:
  128.  
  129. cout << "That is an invalid choice." << endl;
  130. cout << "Please select choices 1-4." << endl;
  131.  
  132. break;
  133. }
  134.  
  135. return 0;
  136. }

im thinking that the layout should be like this:

function prototypes

int main()

loop (on yes)
function calls
-->switch statement

loop (on no)
---> end program

///functions below

it seems that this is something that is very easy--but the switch statment throws me off as im not sure if it should be part of the function or in main itself.

thanks for the help!!
Last edited by afg_91320; Oct 19th, 2008 at 5:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: converting swtich/menu to function

 
0
  #2
Oct 19th, 2008
17 views no replies??

im really stuck here guys!
any guidance would be of great help!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: converting swtich/menu to function

 
0
  #3
Oct 19th, 2008
I usually approach this kind of a problem like:
  1. do
  2. {
  3. menu_func( ) //which may also get the choice and return it
  4.  
  5. switch( user_choice )
  6. {
  7. //do the work here
  8.  
  9. }
  10. }while (user_choice != quit_value );
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 5
Reputation: dumparun is an unknown quantity at this point 
Solved Threads: 1
dumparun's Avatar
dumparun dumparun is offline Offline
Newbie Poster

Re: converting swtich/menu to function

 
0
  #4
Oct 19th, 2008
I really didnt understand your question, as per my understanding..
int main(){
displayMessages();
do{
int choice = displayMenu();
switch(choice)
{
case 1:
doWork();
-
-
while(choice != 'q');
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: converting swtich/menu to function

 
0
  #5
Oct 20th, 2008
basically i take the same code and use functions in it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: converting swtich/menu to function

 
0
  #6
Oct 20th, 2008
^^i followed your layout and this is what i got:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5. int menu(ostream& out);
  6.  
  7.  
  8.  
  9. char choice;
  10.  
  11. int main ()
  12. {
  13.  
  14. double circleArea, rectangleArea, triangleArea,
  15. length, width, height, base, radius;
  16. const double PI = 3.14159;
  17.  
  18. do
  19. {
  20.  
  21. menu(cout);
  22.  
  23. switch (choice)
  24. {
  25.  
  26. case 'C':
  27. case 'c':
  28. case '1':
  29.  
  30. cout << "Enter the radius of the circle: ";
  31. cin >> radius;
  32.  
  33. if (radius < 0)
  34. {
  35. cout << "Number must be greater than 0. Try again" << endl;
  36. }
  37. else
  38. {
  39. circleArea = PI * pow(radius, 2);
  40. cout << "The area of the circle is: " << circleArea;
  41. }
  42.  
  43. cout << endl;
  44.  
  45. break;
  46.  
  47.  
  48. //Calculate the area of a Rectangle.
  49.  
  50. case 'R':
  51. case 'r':
  52. case '2':
  53.  
  54. cout << "Enter the length and width of the rectangle: ";
  55. cin >> length >> width;
  56.  
  57. if (length < 0 || width < 0)
  58. {
  59. cout << "Number must be greater than 0. Try again" << endl;
  60. }
  61. else
  62. {
  63. rectangleArea = length * width;
  64. cout << "The area of the rectangle is: " << rectangleArea;
  65. }
  66.  
  67. cout << endl;
  68.  
  69. break;
  70.  
  71.  
  72. //Calculate the area of a Triangle.
  73.  
  74. case 'T':
  75. case 't':
  76. case '3':
  77.  
  78. cout << "Enter the base and height of the triangle: ";
  79. cin >> base >> height;
  80.  
  81. if (base < 0 || height < 0)
  82. {
  83. cout << "Number must be greater than 0. Try again" << endl;
  84. }
  85. else
  86. {
  87. triangleArea = base * height * .5;
  88. cout << "The area of the triangle is: " << triangleArea;
  89. }
  90.  
  91. cout << endl;
  92.  
  93. break;
  94.  
  95.  
  96. //Quit the program.
  97.  
  98. case 'Q':
  99. case 'q':
  100. case '4':
  101.  
  102. cout << "End of Program" << endl;
  103. cout << "Have a Great Day!" << endl;
  104. cout << endl;
  105.  
  106. break;
  107.  
  108.  
  109. //Invalid number.
  110.  
  111. default:
  112.  
  113. cout << "That is an invalid choice." << endl;
  114. cout << "Please select choices 1-4." << endl;
  115.  
  116. break;
  117.  
  118. return 0;
  119. } while
  120.  
  121. return 0;
  122. }
  123.  
  124. //functions
  125. int menu(ostream& out)
  126. {
  127. out << "\tGeometry Calculator" << endl << endl;
  128. out << setw(30) << "1. Calculate the Area of a [C]ircle" << endl;
  129. out << setw(30) << "2. Calculate the Area of a [R]ectangle" << endl;
  130. out << setw(30) << "3. Calculate the Area of a [T]riangle" << endl;
  131. out << "4. [Q]uit" << endl;
  132.  
  133. out << setw(10) << "Enter your choice (1-4)\n";
  134. cin >> choice;
  135.  
  136. return choice;
  137.  
  138. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 5
Reputation: dumparun is an unknown quantity at this point 
Solved Threads: 1
dumparun's Avatar
dumparun dumparun is offline Offline
Newbie Poster

Re: converting swtich/menu to function

 
0
  #7
Oct 20th, 2008
Couple of suggestions,
1. Avoid using global variables as you used choice here..
2. you can further shrink the main by moving choices to methods here..
3. Display result can also be another method to display the result in generic format
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 737 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC