C ++方程式,用于在一周中的几天内添加工作小时数

时间:2012-04-11 22:21:52

标签: c++ equation

我想弄清楚如何将一周内所有工作时间加起来。 “小时”表示一周工作的小时数,“小时数”表示一天工作的小时数。唯一的问题是弄清楚当它们都用相同的名字表示时如何添加它们。以下是我的代码:(谢谢)

    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;

4 个答案:

答案 0 :(得分:1)

每次输入时,只需将hoursDay添加到小时hours += hoursDay;

不要重复你的代码5次,使用循环

(虽然这看起来像是一个开始练习,所以你可能还没有覆盖循环)

答案 1 :(得分:1)

尝试使用for循环:

int hours=0;
for(int i=0;i<5;i++){
    int hoursday;
    cout << "enter hours worked in day " << i+1 << ":" ;
    while(cin>>hoursday ){
        if(hoursday>0 && hoursday<10){
            hours+=hoursday;
            break;
        }
        else{
            continue;
        }
    }
}

cout <<"total hours in the week : "<<  hours << endl;

答案 2 :(得分:1)

似乎你是初学者,你的程序中有很多错误。但请允许我以不同的方式祝贺你。

您希望程序运行的方式至少需要一个设置为0的变量,然后再添加几天。我发布了一个改进的代码版本。要查看它是否有效,请将其复制并粘贴到编译器中并查看结果。

您的代码应该是这样的。好吧,我的缩进很奇怪......

#include<iostream.h>
#include<conio.h>

void main()
{
    int hoursDay;
        hoursDay=0;
        int hoursday;
        for(int k=1;k<=5;k++)
             {
                 cout<<"Enter hours worked for day"<<k<<"\n";
                 cin>>hoursday;
                 if(hoursday>0&&hoursday<10)
                     {
                             hoursDay=hoursDay+hoursday;
                     }
                 else
                         {
                             cout<<"\ninvalid input";
                            }

                                 }
             int  hours = hoursDay;

             cout << endl;
             cout << endl;
             cout << "Total hours for week is " << hours;
    getch();
}      

答案 3 :(得分:0)

The only problem is figuring out how to add them all when they are all represented by the same name.有几种方法可以解决这个问题,但如果不添加至少一个额外的变量,这些方法都不可能。

一个简单的方法是添加一个新变量int totalHours(或浮点数或双精度数,无论你使用什么)并将其预初始化为零。然后对每个输入设置totalHours += hoursDay;