我试图弄清楚如何将一周内的所有工作时间加起来。“hours”表示一周的工作时间,“hoursDay”表示一天的工作时间。唯一的问题是弄清楚当它们都用相同的名称表示时如何将它们全部添加。以下是我的代码:(谢谢)
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;