Single-Level Control Break

Pages: 12
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  
#include <iostream>
#include <string>
using namespace std;

int main()
{

   const string HEAD1 = " WEEKLY HOURS WORKED";
   const string DAY_FOOTER = " Day Total ";
   // Leading spaces in DAY_FOOTER are intentional.
   const string SENTINEL = "done" 
   double hoursWorked = 0;       		        
   string dayOfWeek;    
   double hoursTotal = 0;      		                
   string prevDay = ""; 		   		
   bool notDone = true; 
      
    int totalHours = hoursWorked + hoursTotal;
        
    cout << " " << endl << " " << endl; 
  
   cout << "\t\t\t\t\t" << HEAD1 << endl;
  
   cout << " " << endl << " " << endl;

  
   
   
cout << "Enter day of week or done to quit: ";
   cin >> dayOfWeek;
   if(dayOfWeek  == SENTINEL)
   {
      notDone = false;
     
   }
       else
   {
      cout << "Enter hours worked: ";
      cin >> hoursWorked;
      cout << dayOfWeek << " " << hoursWorked << endl;
      prevDay = dayOfWeek;
   }	   
		
   while(notDone == true)
   {	
      if(dayOfWeek != SENTINEL)
        {
        	cout << "Enter day of week or done to quit: ";
        	cin >> dayOfWeek;
        	cout << "Enter hours worked: ";
        	cin >> hoursWorked;        	
        	cout << dayOfWeek << " " << hoursWorked << endl;
		}
		else
		{
		  cout << "\t\t" << DAY_FOOTER << hoursTotal + hoursWorked << endl;
		}
		
return 0;
    }


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

Day Total (Tuesday) 8
Is this what you want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;

int main()
{

   const string HEAD1 = " WEEKLY HOURS WORKED";
   const string DAY_FOOTER = " Day Total ";
   // Leading spaces in DAY_FOOTER are intentional.
   const string SENTINEL = "done";
   double hoursWorked = 0;       		        
   string dayOfWeek;    
   double hoursTotal = 0;      		                
   string prevDay = ""; 		   		
   bool notDone = true; 
      
    int totalHours = 0;
        
    cout << " " << endl << " " << endl; 
  
   cout << "\t\t\t\t\t" << HEAD1 << endl;
  
   cout << " " << endl << " " << endl;

      	   
		
   while(notDone == true)
   {	
      
        	cout << "Enter day of week or done to quit: ";
        	cin >> dayOfWeek;
        	
        	if(dayOfWeek == SENTINEL) {
        	 notDone = false;   
        	}
        	
        	else {
        	cout << "Enter hours worked: ";
        	cin >> hoursWorked;        	
        	cout << dayOfWeek << " " << hoursWorked << endl;
        	totalHours += hoursWorked;
        	}
   }
		 cout << "\t\t" << DAY_FOOTER << totalHours << endl;
		
return 0;
    }
It's close

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
So you do want it to show the total hours, or do you want it to show the hours for each day (again, after all input), and then show the total hours?

If you want to it to remember the hours for each day after the fact, you need to store these numbers. But I'm not sure what you're asking.

Edit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#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

Last edited on
Sorry if I'm seem to be vague about it..

it's should be like this...

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
Ah, in that case you need to map arbitrary strings to values.

Give me a sec and I'll edit this post.


Edit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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 (const auto& 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


I left finding the total as an exercise.
Last edited on
Thank you so much :D :D

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.
Last edited on
So it is much better to use std:: rather than "using namespace std" :))

Also, I've come to notice that you used #include <map>...what does it nean and what it is used for??..it is the first time I've seen it
The Standard Library (or what became) wasn't in a namespace initially.
This GotW explains some: http://www.gotw.ca/gotw/053.htm

https://en.wikipedia.org/wiki/Argument-dependent_name_lookup


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).


What is in file that has name "map"?
See http://www.cplusplus.com/reference/map/
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.

A map is a type of data structure, also known as an "associative array", or "dictionary" (Python, C#)
http://www.cplusplus.com/reference/map/map/
https://en.wikipedia.org/wiki/Associative_array
https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(associative_array)

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.
Last edited on
Thanks everyone :d :D
@Ganado

"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?
Last edited on
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.

closed account (E0p9LyTq)
Instead of using namespace std; a "better" way is
1
2
3
using std::cout;
using std::cin;
and so on

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++. using namespace std; defeats those reasons. It is the lazy person's way.
Hi jlb...

I think to really understand what the problem is, I will need to experience it first hand.

Can you write a small piece of code that creates the situation you are worried about?
Sure, try the following with and without that horrible using statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cctype>
#include <vector>
#include <algorithm>

using namespace std;


int main()
{
    std::vector<char> my_vec{'a','b','d'};


    std::transform(my_vec.begin(), my_vec.end(), my_vec.begin(), toupper);
    
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cctype>
#include <vector>
#include <algorithm>
#include<iostream>

using namespace std;


int main()
{
    vector<char> my_vec{'a','b','d'};


    transform(my_vec.begin(), my_vec.end(), my_vec.begin(), ::toupper);
    
    for(int i =0; i < my_vec.size(); i++) {
    cout << my_vec[i] << " ";
    }
    
}
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.
Pages: 12