1週間で働くすべての時間を加算する方法を調べようとしています。 「時間」は1週間に作業した時間を表し、「hoursDay」は1日に作業した時間を表します。唯一の問題は、すべてが同じ名前で表されているときにそれらをすべて追加する方法を考え出すことです。以下は私のコードです:(感謝)C++週の曜日を加算するための計算式
cout << "Enter hours worked for day 1: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 1: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 2: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 2: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 3: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 3: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 4: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 4: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 5: ";
cin >> hoursDay;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 5: ";
cin >> hoursDay;
}
cin.ignore (1);
hours = hoursDay;
cout << endl;
cout << endl;
cout << "Total hours for week is " << hours;
'else {continue; } 'は余分です。もしコードが 'if'を取っていなければ、それらの3行を省略しても繰り返します。 –