0

したがって、以下のコードは 1 つの例外を除いて正常に動作します。

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


int main()
{
    int value1, value2, value3, value4;
    float calcMean,calcStDev;


    // Input values from keyboard
        // prompt user for input
    cout << "Enter 1st value. " << endl;
    cin >> value1;
    cout << "Enter 2nd value. " << endl;
    cin >> value2;
    cout << "Enter 3rd value. " << endl;
    cin >> value3;
    cout << "Enter 4th value. " << endl;
    cin >> value4;

    // Calculate the mean, mean = (value1 + value2 + value3 + value4)/4.0
    calcMean = (value1 + value2 + value3 + value4) / 4.0;

    // Calculate the standard deviation, standard deviation = squareroot((sum((input value-mean)*(input value-mean)))/number of input value - 1)
    calcStDev = sqrt((pow((value1 - calcMean),2) + pow((value2 - calcMean),2) + pow((value3 - calcMean),2) + pow((value4 - calcMean),2))/(4-1));
        // Output display
    cout << fixed << setprecision(2) << endl;
    cout << "Mean of the four values:               " << setw(10) << calcMean << endl;
    cout << "Standard deviation of the four values: " << setw(10) << calcStDev << endl;

    cin.get();
    cin.get();
    return 0;
}

私が期待したように、出力は setw() に応答していないようです (数値 18.50 を 19.64 のすぐ上に重ねて配置したかったのです)。

私が間違っていることは何ですか?:

Enter 1st value.
2
Enter 2nd value.
36
Enter 3rd value.
35
Enter 4th value.
1

Mean of the four values:                                     18.50
Standard deviation of the four values:      19.64
4

1 に答える 1

1

"Mean of the four values: "問題は、文字列リテラルにタブ文字が埋め込まれていることだと思います。SPACEBAR キーの代わりに TAB キーを押してスペースを挿入した可能性があります。

于 2014-02-08T13:32:46.177 に答える