setprecision(2) およびフィールド幅マニピュレータが機能していません。二重減算を実行すると、数値が小数に丸められます。入力が右寄せされていないか、フィールド幅が 6 ではありません。何が間違っていますか?
//Runs a program with a menu that the user can navigate through different options with via text input
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
char userinp;
while (true)
{
cout<<"Here is the menu:" << endl;
cout<<"Help(H) addIntegers(A) subDoubles(D) Quit(Q)" << endl;
cin >> userinp;
userinp = tolower(userinp);
if (userinp == 'h')
{
cout <<"This is the help menu. Upon returning to the main menu, input A or a to add 2 intergers." << endl;
cout <<"Input D or d to subtract 2 doubles. Input Q or q to quit." << endl;
}
else if (userinp == 'a')
{
int add1, add2, sum;
cout <<"Enter two integers:";
cin >> add1 >> add2;
sum = add1 + add2;
cout << setw(6) << setiosflags(ios::right) << "The sum of " << add1 << " + " << add2 << " = " << sum << endl;
}
else if (userinp == 'd')
{
double sub1, sub2, difference;
cout.fixed;
cout <<"Enter two doubles:";
cin >> sub1 >> sub2;
difference = sub1 - sub2;
cout << setw(6) << setiosflags(ios::right) << setprecision(2) << "The difference of " << sub1 << " - " << sub2 << " = " << difference << endl;
}
else if (userinp == 'q')
{
cout <<"Program will exit, goodbye!";
exit(0);
}
else
{
cout <<"Please input a valid character to navigate the menu - input the letter h for the help menu";
cout << "Press any key to continue" << endl;
}
}
}