943,713 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1091
  • C++ RSS
Oct 19th, 2008
0

converting swtich/menu to function

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 19th, 2008
0

Re: converting swtich/menu to function

17 views no replies??

im really stuck here guys!
any guidance would be of great help!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 19th, 2008
0

Re: converting swtich/menu to function

I usually approach this kind of a problem like:
C++ Syntax (Toggle Plain Text)
  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 );
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 19th, 2008
0

Re: converting swtich/menu to function

I really didnt understand your question, as per my understanding..
Quote ...
int main(){
displayMessages();
do{
int choice = displayMenu();
switch(choice)
{
case 1:
doWork();
-
-
while(choice != 'q');
}
}
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dumparun is offline Offline
6 posts
since Jul 2005
Oct 20th, 2008
0

Re: converting swtich/menu to function

basically i take the same code and use functions in it.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 20th, 2008
0

Re: converting swtich/menu to function

^^i followed your layout and this is what i got:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 20th, 2008
0

Re: converting swtich/menu to function

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dumparun is offline Offline
6 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Repetition
Next Thread in C++ Forum Timeline: using input files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC