gpt4 book ai didi

c++ - 在 C++ 中查找用户输入的最小值

转载 作者:行者123 更新时间:2023-11-27 22:33:19 25 4
gpt4 key购买 nike

我的问题是从用户定义的输入数量中找出最小值。到目前为止一切都很好,除了那个。每次我尝试重新排列一些代码或尝试一种新方法时,我总是在屏幕上得到相同的输出:

Sum of numbers entered is:      145.4
Average of numbers entered is: 24.2333
The Highest number entered was: 45
The Lowest number entered was: 6.95283e-310

我每次都会得到这个,不管输入的是什么,或者我尝试过什么不同的建议:

The Lowest number entered was:  6.95283e-310

我知道数组的使用和实现。但是,我正在做的作业甚至还没有涵盖数组。那是后面的一些章节。请不要建议数组...

我看过这里:

Finding Maximum and Minimum values in c++ by user input没用

http://www.cplusplus.com/forum/beginner/38799/没用

改变最大/最小值也不起作用

#include <iostream>
using namespace std;

int main()
{

double number, numberitems, sum = 0, average, max, min;

cout << "Enter number of items: \n";
cin >> numberitems;

//Make sure user can not enter negatives
if ( numberitems < 0 ) {
//no request to perform sum
std::cout << "I said not to enter a negative number... '\n";
std::cin.clear(); //clear bad input flag
return 1;
}

//Get the user's values
for (int i = 0; i < numberitems; i++)
{
std::cout << "Enter any NON-negative number: ";
std::cin >> number;
std::cout << '\n';


//Maximum value entered
if (number > max) {
max = number;
}

//minimum value entered
if (number < min) {
min = number;
}

//Make sure user can not enter negatives
if ( number < 0 ) {
//no request to perform sum
std::cout << "I said not to enter a negative number... '\n";
std::cin.clear(); //clear bad input flag
return 1;
}

//Sum of all the numbers
sum = sum + number;

//Average of all the numbers
average = sum / numberitems;


}
std::cout << endl;
std::cout << endl;
std::cout << "Sum of numbers entered is: " << sum << '\n';
std::cout << "Average of numbers entered is: " << average <<'\n';
std::cout << "The Highest number entered was: " << max <<'\n';
std::cout << "The Lowest number entered was: " << min <<'\n';
return 0;
}

临时修复 :3

    double min = 99999999999999999999999999999999999999999999999;

我对此很陌生。

已更新

阅读更多评论后,我发现我不见了 <cfloat> .使用 #include <cfloat>现在上面每个人的建议都有效。然而,<cfloat>根本没有上课。所以我不确定这在这里是否可用?

#include <iostream>
#include <cfloat>
using namespace std;

int main()
{

int numberitems;
double number, sum = 0, average;
double max = 0;
double min = DBL_MAX;

cout << "Enter number of items: \n";
cin >> numberitems;

//Make sure user can not enter negatives
if ( numberitems < 0 ) {
//no request to perform sum
std::cout << "I said not to enter a negative number... '\n";
std::cin.clear(); //clear bad input flag
return 1;
}

//Get the user's values
for (int i = 0; i < numberitems; i++)
{
std::cout << "Enter any NON-negative number: ";
std::cin >> number;
std::cout << '\n';


//Maximum value entered
if (number >= max) {
max = number;
}

//minimum value entered
if (number <= min) {
min = number;
}

//Make sure user can not enter negatives
if ( number < 0 ) {
std::cout << "I said not to enter a negative number...'\n";
std::cin.clear(); //clear bad input flag
return 1;
}

//Sum of all the numbers
sum = sum + number;

//Average of all the numbers
average = sum / numberitems;

}

//Print the results
// some cosmetic...
std::cout << endl;
std::cout << endl;
std::cout << "\n=================REPORT====================\n";
std::cout << '\n';
std::cout << "\tYour Totals\tValues\n";
std::cout << "\t-----------\t------\n";

std::cout << "\t Sum: " << sum << '\n';
std::cout << "\t Average: " << average <<'\n';
std::cout << "\t Highest: " << max <<'\n';
std::cout << "\t Lowest: " << min <<'\n';
return 0;
}

再次,<cfloat>工作正常,但我不确定是否允许我使用它。还有哪些其他解决方法?

最佳答案

问题已经有了很好的答案 - 很快:minma​​x 未初始化

然而

上面说的很具体。当然,它在这种特定情况下会有所帮助,但我觉得在这里和许多其他情况下都需要更广泛的建议。

添加打印输出

在这种情况下,向您的代码添加调试打印输出(“不使用调试器进行调试”)总是有帮助的。

例如,在您的示例中添加以下内容可能会有所帮助:

//Get the user's values
for (int i = 0; i < numberitems; i++)
{
std::cout << "Enter any NON-negative number: ";
std::cin >> number;
std::cout << '\n';


//Maximum value entered
if (number > max) {
// debug - (remove before submission)
std::cout << "number is the new max! number = " << number
<< ", max = " << max << std::endl;
// end of debug
max = number;
}

//minimum value entered
if (number < min) {
// (debug - remove before submission)
std::cout << "number is the new min! number = " << number
<< ", min = " << min << std::endl;
// end of debug
min = number;
}

关于c++ - 在 C++ 中查找用户输入的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58226572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com