Counting Vectors

Mar 4, 2017 at 2:14am
So I've done some research, and found out I cant count them because it is a type. I cannot figure out how to count whats in it or if I am even thinking of this correctly. I am trying to display the total amount of jobs.

I was previously having trouble getting to get the jobs to display like a list. This code fixed that.

1
2
3
4
5
6
7
  for (const auto & elem : jobs) {
		system ("CLS");	
		cout << "Customer Name     " << "     Job Name" << endl;

		for (int x = 0; x != jobs.size (); ++x) {
			cout << jobs[x] << endl;
		}









Here is my full code.



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <iomanip>
using namespace std;

struct PrintJobs {
	string m_customer = " ";
	string m_jobName = " ";
	int m_front = 0;
	int m_back = 0;
	int m_day = 1;
	int m_quanity = 0;
	//bool m_flash;
	char m_type;
};
ostream& operator << (ostream& os, const PrintJobs& pJ) {
	os.width (23);
	os << left << pJ.m_customer;
	os.width (26);
	os << left << pJ.m_jobName;
	os.width (13);
	os << left << pJ.m_quanity;
	os.width (10);
	os << left << pJ.m_front;
	os.width (10);
	os << left << pJ.m_back;
	//os.width (10);
	//os << left << pJ.m_flash;
	os.width (15);
	os << left << pJ.m_type;
	os.width (10);
	os << left << pJ.m_day;
	return os;
}

int main () {
	bool fQuit = false;
	vector<PrintJobs> jobs{};
	while (!fQuit) {
		cout << "1. Enter job \t2. Show all jobs \t3. Search jobs by day \t4. Quit";
		cout << "\nEnter your choice \n";
		int choice{};
		cin >> choice;//input validation required
		cin.ignore ();
		switch (choice) {
		case 1:
		{
			PrintJobs temp{};
			cout << "Customer name \n";
			getline (cin, temp.m_customer);
			cout << "Job name \n";
			getline (cin, temp.m_jobName);
			cout << "Quanity \n";
			cin >> temp.m_quanity;
			cout << "Front \n";
			cin >> temp.m_front;
			cout << "Back \n";
			cin >> temp.m_back;
			//cout << "Flash y/n \n";
			//cin >> temp.m_flash;
			cout << "Type \n";
			cin >> temp.m_type;
			cout << "Enter day number (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
			cin >> temp.m_day;
			cin.ignore ();
			jobs.push_back (move (temp));
			system ("PAUSE");
			system ("CLS");
		}
		break;
		case 2:
		{
			if (jobs.size () == 0) {
				cout << "No jobs to display \n";
				system ("PAUSE");
				system ("CLS");
			}
			else {
				
				for (const auto & elem : jobs) {
					system ("CLS");	
					cout << "Customer Name     " << "     Job Name" << endl;

					for (int x = 0; x != jobs.size (); ++x) {
						cout << jobs[x] << endl;
					}

					//cout << elem << endl << endl;
					
					system ("PAUSE");
					system ("CLS");
				}
			}
		}
		break;
		case 3:
		{
			if (jobs.size () == 0) {
				cout << "No jobs to search \n";
				system ("PAUSE");
				system ("CLS");
			}
			else {
				cout << "Enter day number to search: (Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6), Sun(7)) \n";
				int daySearch{};
				cin >> daySearch;
				cin.ignore ();
				cout << "Searching ... \n";
				bool match = false;
				for (const auto & elem : jobs) {
					if (elem.m_day == daySearch) {
						cout << elem << "\n";
						match = true;
					}
				}
				if (match == false) {
					cout << "No matches were found \n";
					system ("PAUSE");
					system ("CLS");
				}
			}
		}
		break;
		case 4:
			fQuit = true;
			cout << "Goodbye \n";
			break;
		default:
			cout << "Incorrect choice, try again \n";
			break;
		}
	}
}

Last edited on Mar 4, 2017 at 2:15am
Mar 4, 2017 at 2:53am
Something like this, perhaps:

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
63
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

struct PrintJobs {

	std::string m_customer ;
	std::string m_jobName ;
	int m_front = 0;
	int m_back = 0;
	int m_day = 1;
	int m_quanity = 0;
	char m_type = 'X' ;
};

std::ostream& operator <<( std::ostream& os, const PrintJobs& pJ ) {

	return os << std::setw(20) << pJ.m_customer
	          << std::setw(25) << pJ.m_jobName
	          << std::setw(8) << pJ.m_quanity
	          << std::setw(8) << pJ.m_front
	          << std::setw(8) << pJ.m_back
	          << std::setw(6) << pJ.m_type
	          << std::setw(8) << pJ.m_day ;
}

std::ostream& print_header( std::ostream& os ) {

	return os << std::setw(20) << "customer"
	          << std::setw(25) << "job name"
	          << std::setw(8) << "quanity"
	          << std::setw(8) << "front"
	          << std::setw(8) << "back"
	          << std::setw(6) << "type"
	          << std::setw(8) << "day"
	          << '\n' << std::string( 90, '-' ) << '\n' ;
}


int main() {

    std::vector<PrintJobs> jobs { { "customer one", "job one", 1, 2, 3, 4, 'A' },
                                  { "customer two", "job two", 5, 6, 7, 8, 'B' },
                                  { "customer three", "job three", 9, 10, 11, 12, 'A' },
                                  { "customer one", "job four", 13, 14, 15, 16, 'C' },
                                  { "customer two", "job five", 17, 18, 19, 20, 'B' }
                                } ;
    int total_quanity = 0 ;

    std::cout << "     " ;
    print_header(std::cout) ;

    int slno = 0 ;
    for( const auto& j : jobs ) {

        std::cout << std::setw(3) << ++slno << ". " << j << '\n' ;
        total_quanity += j.m_quanity ;
    }

    std::cout << "\ntotal number of jobs:" << std::setw(5) << jobs.size()
              << "\n       total quanity:" << std::setw(5) << total_quanity << '\n' ;
}

http://coliru.stacked-crooked.com/a/0942e73983e15fe3
Topic archived. No new replies allowed.