gpt4 book ai didi

C++ 循环查找用户输入的最小值、最大值、平均值和总和

转载 作者:太空宇宙 更新时间:2023-11-04 16:15:33 24 4
gpt4 key购买 nike

我被要求编写一个程序来查找用户输入的最小值、最大值、平均值和总和,我想出了以下程序,但我使用的循环似乎有问题,因为程序刚刚退出编译时。有人可以将错误指出给我吗?非常感谢。

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
bool empty = true;
int max, min, count, avg, sum=0;
char choice;

for (count = 0; count++;)

{

do
{
cout << "Enter a number: ";

int num;
cin >> num;

if (empty)
{
empty = false;
min = num;
max = num;
}
else if (max < num)
{
max = num;
}
else if (num < min)
{
min = num;
}

sum = sum + num;

avg = sum / count;

cout << "Do you want to continue?: ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');

if (!empty)
{
cout << " Maximum is " << max << endl;
cout << " Minimum is " << min << endl;
cout << " Sum is " << sum << endl;
cout << " Average is " << avg << endl;
}
}
return 0;
}

最佳答案

您的 for 循环不正确:

for (count = 0; count++;)

记住 for 循环是:

for (initialization; expression; update)

count++count 加 1 但返回其原始值 0,该值被转换为 false。因为 expression 为假,循环停止。

关于C++ 循环查找用户输入的最小值、最大值、平均值和总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173948/

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