[英]Need help in basic C++ regarding how to properly loop through part and finding smallest value
嗨,我需要一些帮助。 我正在介绍编程课,我们正在使用c ++。 我希望有人可以帮我完成昨天的作业(我知道不要期待奇迹般的反应,但是女孩总是可以尝试的)。
我有两个我知道的问题。 首先是关于最小值。 最重要的是尝试使其循环达到三倍的需求,但又不输给我。 我不能使用数组或任何我还没有学到的东西,这就是为什么我发布了这个。 我见过类似的问题和问题,但最后得出的答案太复杂,无法满足当前的课堂学习要求。 因此,这里是问题说明:
说明1)编写一个程序,以查找作为键盘输入提供的一组数字的平均值,最大值和最小值。 数据集中的值的数量必须在0到20之间(包括0和20)。 用户将首先在数据集中输入值的数量(使用变量int Number)。 给用户3次尝试在给定范围内输入数字。 如果输入的数字值超出此范围,请写一条错误消息,然后继续。 如果用户在3次尝试中都没有输入有效的Number值,则打印一条错误消息并终止程序。
2)打印时,仅将“平均值”的输出格式化为小数点后3位。
3)在输入作为输入数据组中的值可以是任何值正,负或零。
4)使程序输出可读(请参见下面的示例)。 (请注意:您将不会像通常需要的那样打印出在程序中输入的输入值。这是因为在我们的研究中,我们尚未涵盖这样做所需的“工具”)。
下面是程序执行的输出:(使用这些值以获取数据集-> 19.0 53.4 704.0 -15.2 0 100.0
)
The largest number: 704
The smallest number: -15.2
The average of the 6 numbers entered: 143.533
yourName L4p2XX.cpp
Lab#4 prob 2 XX-XX-12
这是我在解决方案上的可怜借口:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double Number = 0, minValue, maxValue, average, total = 0;
int ct = 0, numCount;
cout << "How many numbers would you like to enter? ";
cin >> numCount;
for(ct = 1; ct <= numCount; ct += 1)
{
cout << "Enter Value from 0 to 20, inclusive: ";
cin >> Number;
if(Number > 20|| Number < 0)
for(int errorCt = 1; errorCt <= 4; errorCt += 1)
{
if(errorCt == 4)
{
cout << "You have had 3 attempts to enter a valid" <<
"number. \nPlease try this program again when you" <<
"are able to follow directions.";
cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
return 0;
}
cout << Number << "is not within range.\n" <<
"Please enter a number from 0 to 20: ";
cin >> Number;
} //end for loop
total += Number;
if(maxValue <= Number)
maxValue = Number;
if(Number <= minValue)
minValue = Number;
} //end for loop
cout << "The smallest number entered was " << minValue << endl;
cout << "The largest number you entered was " << maxValue << endl;
average = total/numCount;
cout << setprecision(3) << fixed << showpoint << "You entered " <<
numCount << " numbers. The average of these is " << average;
//Program ID
cout <<"\n" << "L4P2LB.cpp\n" << "11-05-12\n";
system ("pause");
return 0;
} // End main
预先感谢任何可以引导我朝正确方向发展的人。 不找人做我的工作,如果没有其他建议或关于如何做的任何建议,我只需要指导方面的帮助。 再次感谢。 琳达
我还需要某种方式在第三次之后暂停并正确退出。 如果我在其中添加第二个暂停将不起作用,那么我是否也错过了明显的东西!
我看到的第一个问题是您没有初始化几个变量。
您应该使用在每种情况下都会在第一个循环中覆盖的值(通常是“正/负无穷大”,由<limits>
)来初始化minValue
和maxValue
变量,或者在第一次迭代中都将它们都设置为Number
,无论其当前值。 所以我建议通过替换来解决此问题
if(maxValue <= Number)
maxValue = Number;
if(Number <= minValue)
minValue = Number;
与
if(maxValue <= Number || ct == 1)
maxValue = Number;
if(Number <= minValue || ct == 1)
minValue = Number;
因为ct == 1
在第一次迭代中为true。
也就是说,您检查了错误变量的0..20
范围条件。 您在Number
变量上进行了检查,但应检查numCount
变量。 但是,您也没有遵守存储“数字数量”的变量应为Number
,因此您确实检查了正确的变量,但是使用了错误的输入输入。 这应该可以解决此问题(我在cin >>...
行中更改了变量名,并将检查移到了主循环之外):
cout << "How many numbers would you like to enter? ";
cin >> Number;
if(Number > 20|| Number < 0)
{
for(int errorCt = 1; errorCt <= 4; errorCt += 1)
...
if(errorCt == 4)
{
cout << "You have had 3 attempts to enter a valid" <<
"number. \nPlease try this program again when you" <<
"are able to follow directions.";
cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
return 0;
}
cout << Number << "is not within range.\n" <<
"Please enter a number from 0 to 20: ";
cin >> Number;
} //end for loop
}
for(ct = 1; ct <= Number; ct += 1)
{
...
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.