[英]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.