想求它输入A,10时输出199.90

#include
using namespace std;
int main()
{
double x;
char c1;
cin>>c1>>x;
if(c1=='A') cout<<x19.99<<endl;
if(c1=='B') cout<<x
17.99<<endl;
if(c1=='C') cout<<x15.99<<endl;
if(c1=='D') cout<<x
11.99<<endl;
if(c1!='A'&&c1!='B'&&c1!='C'&&c1!='D') cout<<"No avialable."<<endl;
return 0;
}

使用 setiosflags(ios::fixed) << setprecision(N) 保留小数点后N位的数字。

另外,建议使用:

if ()
{}
else if ()
{}
else if ()
{}
else
{}

完整代码如下:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    double x;
    char c1;
    cin >> c1 >> x;
    if(c1 == 'A') cout << setiosflags(ios::fixed) << setprecision(2) << x * 19.99 << endl;
    else if(c1 == 'B') cout << setiosflags(ios::fixed) << setprecision(2) << x * 17.99 << endl;
    else if(c1 == 'C') cout << setiosflags(ios::fixed) << setprecision(2) << x * 15.99 << endl;
    else if(c1 == 'D') cout << setiosflags(ios::fixed) << setprecision(2) << x * 11.99 << endl;
    else cout << "No avialable." << endl;
    return 0;
}

img

img