Ok so just to let you know that this is a homework question so don't solve it for me but give me some hint on how to convert it to switch statement. Thank you.
Ok wait, I don't understand what you mean but here's the full question and my answer is the coding above:
Create a program that converts the following if ladder statement into a switch statement. Use the program input, processing and output statements and sample input to find the rest of the details to create your program.
if (precision == 1)
{
cout << fixed << showpoint << setprecision(2);
}
else if (precision == 2 || precision == 3)
{
cout << fixed << showpoint << setprecision(4);
}
else if (precision == 4)
{
cout << fixed << showpoint << setprecision(6);
}
else
{
cout << fixed << showpoint << setprecision(8);
}
Input – One user double, one whole number to select precision.
Process – Set the number of decimal places to show on the double, in a switch.
Output – The user’s double with as many decimal places set by the precision.
Programming Details: I am expecting one “fall through” in the switch statement. The iomanip library holds setprecision and you will need to include it to get setprecision to work. You should have just one cout statement after the switch.
Hey, don't get mad please.. I tried what you said and it pretty much works except that it does not limit the number of decimal places.. Thank you though you solved 90% of my problem.
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int choice;
double(number);
cout << "Enter a whole number" << endl;
cin >> number;
cout << "Pick the precision to show the input (1-5)" << endl;
cout << "1. 1" << endl;
cout << "2. 2" << endl;
cout << "3. 3" << endl;
cout << "4. 4" << endl;
cout << "5. 5" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Showing the input:" << number << fixed << showpoint << setprecision(2) << endl;
break;
case 2:
cout << "Showing the input:" << number << fixed << showpoint << setprecision(4) << endl;
break;
case 3:
cout << "Showing the input:" << number << fixed << showpoint << setprecision(4) << endl;
break;
case 4:
cout << "Showing the input:" << number << fixed << showpoint << setprecision(6) << endl;
break;
default:
cout << "Showing the input:" << number << fixed << showpoint << setprecision(8) << endl;
}