The program should produce a report for a supermarket manager to help her keep track of the hours worked by her part-time employees. The report should include the day of the week and the total hours worked by all employees each day.
Expected Output:
Enter day of week or done to quit: Monday
Enter hours worked: 2
Monday 2
Enter a day of week or done to quit: Monday
Enter hours worked: 4
Monday 4
Enter a day of week or done to quit: Tuesday
Enter hours worked: 8 ===============>// until this point, my output is fine.
Enter a day of week or done to quit: DONE ======>> but in this part, this is the only part where I am having trouble with..I just can't seem to get this output
Day Total (Monday) 6
Tuesday 8
Enter a day of week or done to quit: done
Expected Output:
Enter day of week or done to quit: Monday
Enter hours worked: 2
Monday 2
Enter a day of week or done to quit: Monday
Enter hours worked: 4
Monday 4
Enter a day of week or done to quit: Tuesday
Enter hours worked: 8
Enter a day of week or done to quit: DONE ===>> in this part, instead of only just showing totals hours for all the days....
Day Total (Monday) 6 ====> it should be like this where the totals hours is shown right beside their respective day.
Day Total (Tuesday) 8
#include <vector>
#include <iostream>
#include <string>
struct Day {
std::string name;
double hours_worked;
};
int main()
{
std::vector<Day> days;
bool done = false;
while (!done)
{
std::cout << "Enter day of the week: ";
std::string day_name;
std::cin >> day_name; // Note: Does not prevent duplicate days!
if (day_name == "done")
{
done = true;
}
else
{
std::cout << "Enter hours worked: ";
double hours_worked;
std::cin >> hours_worked;
days.push_back( {day_name, hours_worked} );
}
}
std::cout << std::endl;
for (size_t i = 0; i < days.size(); i++)
{
std::cout << "Day: " << days[i].name << ", Hours worked: " << days[i].hours_worked << '\n';
}
std::cout << "TODO: Re-implement total" << std::endl;
}
Enter day of the week: A
Enter hours worked: 3
Enter day of the week: B
Enter hours worked: 4
Enter day of the week: C
Enter hours worked: 5
Enter day of the week: D
Enter hours worked: 6
Enter day of the week: done
Day: A, Hours worked: 3
Day: B, Hours worked: 4
Day: C, Hours worked: 5
Day: D, Hours worked: 6
Enter day of the week: A
Enter hours worked: 3
Enter day of the week: A
Enter hours worked: 4
Enter day of the week: C
Enter hours worked: 5
Enter day of the week: D
Enter hours worked: 6
Enter day of the week: done
Day: A, Hours worked: 7 ===> 'A' was put twice...so whatever their hours worked respectively to that day should be added together.
Day: C, Hours worked: 5
Day: D, Hours worked: 6
#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<std::string, int> days;
bool done = false;
while (!done)
{
std::cout << "Enter day of the week: ";
std::string day_name;
std::cin >> day_name;
if (day_name == "done")
{
done = true;
}
else
{
std::cout << "Enter hours worked: ";
int hours_worked;
std::cin >> hours_worked;
// Adds the hours worked to the specified day.
// If the day's string isn't in the map yet,
// it is implicitly default constructed with a mapped value of 0,
// and then added on to.
days[day_name] += hours_worked;
}
}
std::cout << std::endl;
for (constauto& day_hour_pair : days )
{
std::cout << "Day: " << day_hour_pair.first << ", Hours worked: " << day_hour_pair.second << '\n';
}
}
D:\code\cplusplus243635>main
Enter day of the week: A
Enter hours worked: 3
Enter day of the week: A
Enter hours worked: 4
Enter day of the week: B
Enter hours worked: 3
Enter day of the week: C
Enter hours worked: 5
Enter day of the week: A
Enter hours worked: 100
Enter day of the week: done
Day: A, Hours worked: 107
Day: B, Hours worked: 3
Day: C, Hours worked: 5
Quesion...
I've heard that using 'using namespace std' is wrong when coding? that it's is much prefer to use std :: ...why?? does it change the performance of the program?
It isn't wrong, per se. And no, "using namespace std;" has no impact on performance (all of that is determined at compile-time).
You can use it if you want, but be aware that it pollutes every function or object that is declared within the std namespace to now be in the global namespace.
"using namespace std;" should NEVER be put in header files, but it's OK to put in implementation (.cpp) files as long as you know what you're doing.
It can lead to ambiguities if you use an identifier that's already used within the std namespace, such as distance or complex or other things. It's usually fine, just don't put it in header files.
You can also do things like using std::map, std::string, std::cin, std::cout; to only put certain identifiers into the global namespace.
What is #include <map> used for?
For exactly the same reasons as you had #include <iostream> Why did you?
#include <foo>
tells the preprocessor to seek file named "foo" and replace the #include-line with the content of that file (on the fly, no source files are harmed while compiling).
The C++ standard library's classes and functionality are divided into different headers.
Each header declares certain features you can use. <map> includes the definite for the map class.
Within a map is some container that holds key-value pairs, e.g. { "string", 42 } if it's a map<string, int>. It's usually implemented in C++ as a tree.
"using namespace std;" should NEVER be put in header files, but it's OK to put in implementation (.cpp) files as long as you know what you're doing.
I took your advice and removed the "using namespace std;" from a header file I was working on. I really hate typing out std:: in front of everything. So why was it wrong to save a butt load of time and just leave the using namespase std; in the header file?
Some say it is a bad practice... but if I know for certain that I am only using the standard c++ library and nothing fancy, then I tend to make my code stink of "using namespace std;". It is only if I am mixing libraries or other namespaces, then I agree that "using namespace std;" is bad.
It is only if I am mixing libraries or other namespaces, then I agree that "using namespace std;" is bad.
Do you realize that there are two namespaces in use when writing a "simple" C++ program? The std namespace and the global namespace.
Do you realize that there can be name collisions between the global namespace and the std namespace caused by using different parts of only standard C++ headers and functions?
And do you realize that the error messages for these collisions tend to be very obtuse.
Do you realize that if you use any using statements in the global section of a header forces anyone using your header into using these global using statements?
I really suggest that you get over your laziness and get used to using the namespace qualifiers when required, after a little while you will start getting used to them and if they are missing you will go crazy.
If you insist on using "using" statements then make sure you place these statements into as limited scopes as possible, such as single functions and maybe even inside more limited scopes as well.
This ensures you have as few namespace clashes as possible, so easier to track down errors that occur.
Personally I find it easier to just type the std:: qualifier, it has become an unconscious habit that even when I use/modify code from someone else that has using statements I still type the fully qualified operator.
There are good reasons why namespaces were introduced in C++. usingnamespace std; defeats those reasons. It is the lazy person's way.
So my counter argument is, don't mix namespaces. But if you feel you must, then you can still save on code typing by using namespace std; or whatever and for that odd jerk that belongs to the global namespace of some other, then you can go ahead and access his source. In the above example ::toupper comes from the global namespace, so we got him from there but everything else was std.