Reputation: 1
I am trying to figure out how to add up all hours worked in a week. "hours" represents hours worked in a week and "hoursDay" represents the hours worked in a day. The only problem is figuring out how to add them all when they are all represented by the same name. Below is my code: (thanks)
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;
Upvotes: 0
Views: 1677
Reputation: 443
Seems you are a beginner, there are lots of errors in you program. But let me congratulate you on thinking differently.
The way you want your program to behave you need at least a variable set to 0 initially and then add up days to it. I am posting an improved version of your code. To see if it's effective, copy it and paste in compiler and see the result.
Your code should be like this. Well my indentation is weird...
#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();
}
Upvotes: 1
Reputation: 3351
Try to use a for loop:
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;
Upvotes: 1
Reputation: 14751
Just add hoursDay to hours each time you input it hours += hoursDay;
And don't repeat your code 5 times, use a loop
(although this looks like a beginning exercise, so you might not have covered loops yet)
Upvotes: 1
Reputation: 39847
The only problem is figuring out how to add them all when they are all represented by the same name.
There are a few ways you can solve this but none of them are possible without adding at least one an additional variable.
A simple method is to add a new variable, int totalHours
(or float or double, whatever you're using) and pre-initialize it to zero. Then with each input, set totalHours += hoursDay;
.
Upvotes: 0