私の前スレを見てくださった方、前回はごめんなさい。うっかりミスやタイプミスだらけでした。これは私の課題です:
「ユーザーが入力ステートメントを介して一連の負でない数を入力できるようにするプログラムを作成します。入力プロセスの最後に、プログラムは次のように表示します: 奇数の数とその平均; 偶数の数およびその平均; 入力された数値の総数。負の値を入力して入力プロセスを停止できるようにします。この終了条件がユーザーに通知されていることを確認してください。
そして、ここに私のコードがあります:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number, total1=0, total2=0, count1=0, count2=0;
do
{
cout << "Please enter a number. The program will add up the odd and even ones separately, and average them: ";
cin >> number;
if(number % 2 == 0)
{
count1++;
total1+=number;
}
else if (number >= 0)
{
count2++;
total2+=number;
}
}
while (number>=0);
int avg1 = total1/count1;
int avg2 = total2/count2;
cout << "The average of your odd numbers are: " << avg1 << endl;
cout << "The average of your even numbers are " << avg2 << endl;
}
正常に動作しているように見えますが、プログラムを終了するために負の数を入力すると、残りの平均化された数に含まれます。これを回避するためのアドバイスはありますか?私はそれが可能であることを知っていますが、その考えは私を逃れます.