Question

In: Computer Science

I need my code output values edited to be formatted to two decimal places (i believe...

I need my code output values edited to be formatted to two decimal places (i believe using setprecision), i will need you to edit my code toformat each menu option to 2 decimal places: provided is my code

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   double radius;
   double base;
   double height;
   double length;
   double width;
   double Area;
   const double PI = 3.14159;
   int choice;

   // display menu
   cout << "Geometry Calculator" << endl << endl;
   cout << " 1. Calculate the Area of a Circle" << endl;
   cout << " 2. Calculate the Area of a Rectangle" << endl;
   cout << " 3. Calculate the Area of a Triangle" << endl;
   cout << " 4. Quit" << endl << endl;
   cout << "Enter your choice (1-4):" << endl;
   cin >> choice;

   //validate and process the menu

   switch(choice)
   {
       case 1:

           cout << "\n";
           cout << "Enter the radius of the circle: ";
           cin >> radius;

           if (radius > 0)
           {
               Area = PI * radius * radius;

               cout << "\n";
               cout << "Area of Circle is: " << Area << setprecision(2) << endl;

           }
           else
           {
               cout << "The radius can not be less than zero. Only enter positive values for your radius.";
           }
           break;

       case 2:

           cout << "\n";
           cout << "Enter the length of the rectangle: ";
           cin >> length;
           cout << "Enter the width of the rectangle: ";
           cin >> width;

           if (length > 0 && width > 0)
           {
               Area = length * width;

               cout << "\n";
               cout << "Area of Rectangle is: " << Area << endl;
           }
           else
           {
               cout << "\nOnly enter positive values for length and width.";
           }
           break;

       case 3:


           cout << "\n";
           cout << "Enter the base of the Triangle: ";
           cin >> base;
           cout << "Enter the height of the Triangle: ";
           cin >> height;

           if (base > 0 && height > 0)
           {
               Area = 0.5 * base * height;

               cout << "\n";
               cout << "Area of the Triangle is: " << Area << endl;
           }
           else
           {
               cout << "\nOnly enter positive values for base and height.";
           }
           break;

       case 4:

           cout << "Exiting Geometry Calculator\nGoodbye!\n";
           break;

       default: cout << "Sorry, that was an invalid entry!\nPlease re-run the program and "
           << "enter a valid menu choice.\n";
}
return 0;
}

Solutions

Expert Solution

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   double radius;
   double base;
   double height;
   double length;
   double width;
   double Area;
   const double PI = 3.14159;
   double tmp;
   int choice;

    cout<<fixed<<setprecision(2);
   // display menu
   cout << "Geometry Calculator" << endl << endl;
   cout << " 1.00 Calculate the Area of a Circle" << endl;
   cout << " 2.00 Calculate the Area of a Rectangle" << endl;
   cout << " 3.00 Calculate the Area of a Triangle" << endl;
   cout << " 4.00 Quit" << endl << endl;
   cout << "Enter your choice (1.00-4.00):" << endl;
   cin >> tmp;
   choice = (int)tmp;

   //validate and process the menu

   switch(choice)
   {
       case 1:

           cout << "\n";
           cout << "Enter the radius of the circle: ";
           cin >> radius;

           if (radius > 0)
           {
               Area = PI * radius * radius;

               cout << "\n";
               cout << "Area of Circle is: " << Area << setprecision(2) << endl;

           }
           else
           {
               cout << "The radius can not be less than zero. Only enter positive values for your radius.";
           }
           break;

       case 2:

           cout << "\n";
           cout << "Enter the length of the rectangle: ";
           cin >> length;
           cout << "Enter the width of the rectangle: ";
           cin >> width;

           if (length > 0 && width > 0)
           {
               Area = length * width;

               cout << "\n";
               cout << "Area of Rectangle is: " << Area << endl;
           }
           else
           {
               cout << "\nOnly enter positive values for length and width.";
           }
           break;

       case 3:


           cout << "\n";
           cout << "Enter the base of the Triangle: ";
           cin >> base;
           cout << "Enter the height of the Triangle: ";
           cin >> height;

           if (base > 0 && height > 0)
           {
               Area = 0.5 * base * height;

               cout << "\n";
               cout << "Area of the Triangle is: " << Area << endl;
           }
           else
           {
               cout << "\nOnly enter positive values for base and height.";
           }
           break;

       case 4:

           cout << "Exiting Geometry Calculator\nGoodbye!\n";
           break;

       default: cout << "Sorry, that was an invalid entry!\nPlease re-run the program and "
           << "enter a valid menu choice.\n";
}
return 0;
}


Related Solutions

I need an to check my answer with 4 decimal places: Better Mousetraps has developed a...
I need an to check my answer with 4 decimal places: Better Mousetraps has developed a new trap. It can go into production for an initial investment in equipment of $5.4 million. The equipment will be depreciated straight line over 6 years to a value of zero, but in fact it can be sold after 6 years for $682,000. The firm believes that working capital at each date must be maintained at a level of 10% of next year’s forecast...
Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
Fill in the missing values for this ANOVA summary table round to two decimal places: S.S....
Fill in the missing values for this ANOVA summary table round to two decimal places: S.S. d.f. M.S. F Between 2371.488 6 Within TOTAL 5843.488 37 S.S. d.f. M.S. F Between 576.45 3 Within TOTAL 2276.45 37 S.S. d.f. M.S. F Between 107.07 Within 5160 TOTAL 5695.35 65 S.S. d.f. M.S. F Between 7.664 Within 430 10 TOTAL 50
I need to write a financial literacy plan for my life that I believe, if followed,...
I need to write a financial literacy plan for my life that I believe, if followed, will allow me to live a responsibly healthy financial life and retire in a financially sound position. What are some things I can discuss in it?
Files I need to be edited: I wrote these files and I put them in a...
Files I need to be edited: I wrote these files and I put them in a folder labeled Project 7. I am using this as a study tool for a personal project of mine. //main.cpp #include <iostream> #include "staticarray.h" using namespace std; int main( ) {    StaticArray a;    cout << "Printing empty array -- next line should be blank\n";    a.print();    /*    // Loop to append 100 through 110 to a and check return value    // Should print "Couldn't append 110"...
How do I make this code print the st variable rounded to 2 decimal places? Everything...
How do I make this code print the st variable rounded to 2 decimal places? Everything I have tried gives me an error. print('Enter output filename') name=input() print('Enter principal amount') p=float(input()) print('Enter term length (month)') n=int(input()) print('Enter annual interest') i=float(input()) j=i/12 m=p*j/(1-(1+j)**-n) file = open(name, "w") title = ["Month, ", "Total Accured interest, ", "Loan Balance \n"] file.writelines(title) first_row=["0",",","$","0.00",",","$",p," \n"] s1=' '.join([str(elem) for elem in first_row]) file.writelines(s1) t=0 for j in range(n): tai=(p*i/12) t=t+tai p=(p-m)+tai st="" ls=[j+1,",","$",t,",","$",p,"\n"] st=''.join([str(elem) for elem...
hi i need a code that will give me this output, For the multiply_list, the user...
hi i need a code that will give me this output, For the multiply_list, the user will be asked to input the length of the list, then to input each element of the list. For the repeat_tuple, the user is only asked to enter the repetition factor, but not the tuple. Your program should take the list created before and convert it to a tuple. output expected: (**user input**) ******Create your List ****** Enter length of your list: 3 ******...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
Calculate to 3 decimal places the charge on a-melanotropin at pH values of 11, 5, and...
Calculate to 3 decimal places the charge on a-melanotropin at pH values of 11, 5, and 1 please do not use titration curve explanation - i dont understand it and it wont help - i have the answers - i do not understand how to get them however - which values have negative and positive charges? and at what pHs? thats what confuses me. TIA
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT