Just a hint please

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.

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);

}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int number;
double(precision);
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 >> precision;
switch (precision)
{
case 1:
cout << fixed << showpoint << setprecision(2);
break;

case 2:
cout << fixed << showpoint << setprecision(4);
break;
case 4:
cout << fixed << showpoint << setprecision(6);
break;
default:
cout << fixed << showpoint << setprecision(8);
}

return 0;
}





This is what i have written and i know it is wrong but i think i have got the right idea
Oh! I forgot to put that in but still even whithout it, it should work and it is not working... That's what is bothering me the most.
My question is that what am i missing in this code, because it is not working:


#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int number;
double(precision);
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 >> precision;
switch (precision)
{
case 1:
cout << fixed << showpoint << setprecision(2);
break;

case 2: case 3:
cout << fixed << showpoint << setprecision(4);
break;
case 4:
cout << fixed << showpoint << setprecision(6);
break;
default:
cout << fixed << showpoint << setprecision(8);
}

return 0;
}
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.
Last edited on
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;
}

return 0;
}
This is my new code and it runs perfectly fine thank you very very much, you saved my life.

#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int choice;
double number = 3.14579;
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:" << fixed << showpoint << setprecision(2) << number << endl;
break;

case 2:
cout << "Showing the input:" << fixed << showpoint << setprecision(4) << number << endl;
break;
case 3:
cout << "Showing the input:" << fixed << showpoint << setprecision(4) << number << endl;
break;
case 4:
cout << "Showing the input:" << fixed << showpoint << setprecision(6) << number << endl;
break;
default:
cout << "Showing the input:" << fixed << showpoint << setprecision(8) << number << endl;
}

return 0;
}
Topic archived. No new replies allowed.