1 週間のすべての作業時間を合計する方法を見つけようとしています。「hours」は 1 週間の労働時間を表し、「hoursDay」は 1 日の労働時間を表します。唯一の問題は、それらがすべて同じ名前で表されている場合に、それらをすべて追加する方法を考え出すことです。以下は私のコードです:(ありがとう)
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;