我目前正在研究子例程/子程序或任何你用英语称呼它们的东西,在我试图解决的这个特定作业中,我必须计算两个单词的平均长度。
\nvoid Length(string const text_1,\n string const text_2,\n int & total_length,\n double & mean_length)\n{\n total_length = text_1.length() + text_2.length();\n mean_length = static_cast<double>(total_length) / 2;\n}\n\nvoid Length_Program(int val)\n{\n string text_1;\n string text_2;\n int total_length{};\n double mean_length{};\n\n cout << "Mata in tv\xc3\xa5 ord: ";\n cin >> text_1 >> text_2;\n cout << "Totall\xc3\xa4ngd: ";\n Length(text_1, text_2, total_length, mean_length);\n cout << total_length << endl;\n cout << "Medell\xc3\xa4ngd: " << fixed << setprecision(1) << mean_length;\n\n}\nRun Code Online (Sandbox Code Playgroud)\n我已将精度设置为 set precision(1),我假设它只会写一位小数,但我一直得到两位小数。\n我的例子是: abcd E\nit 应该说它是 2.5 个单词的平均值,但它说 2.51一些原因。有人可以帮助我理解我做错了什么吗? …