Hi
I'm self-testing to see what I understand of C++ thus far, in particular Vectors and Pairs.
(I'm trying to incorporate the use of a Vector comprised of Pairs)
Scenario:
A JOB BOARD displays available deliveries. Between 1 and 5 categories are listed.
Within a FOR loop, the programme determines in each case whether to display that category.
Each Category is assigned a PAIR containing a string variable and an INT variable.
e.g. "Greenhouse",100
I'm hopefully then correctly building--and later properly accessing--the PAIRs in that Vector.
Within the FOR loop I attempt to use rand() to determine first whether to list each of the five choices.
If the item is chosen for display I then attempt to update the PAIR item's INT value by multiplying it with a number determined by a separate rand() call.
Here's the 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
|
#include <iostream>
#include <string>
#include <vector>
#include <time.h> // for random number generator, but I'm not using it in this Sample
#include <utility> // for pair
#include <math.h>
using namespace std; // I'm not using most of this but I'm keeping the code clean for now; I can add std:: later
// Declaring VARIABLES
int firstChoice;
int ifListed;
int jobsPay;
int jobsProfit;
int i;
// END Declaring VARIABLES
int main() {
// JOB BOARD
cout << "************************************************************************" << endl;
cout << "************************** JOB BOARD ******************************" << endl;
cout << "************************************************************************\n\n";
// First declare pairs, then make vector of those pairs
pair<string, int> delivery1 ("Clothiers",100);
delivery1.first = "Clothiers"; // are these two lines
delivery1.second = 100; // necessary?
delivery1 = make_pair(string("Clothiers"), 100);
pair<string, int> delivery2;
delivery2.first = "Greenhouse";
delivery2.second = 200;
delivery2 = make_pair(string("Greenhouse"), 200);
pair<string, int> delivery3;
delivery3.first = "Factory";
delivery3.second = 300;
delivery3 = make_pair(string("Factory"), 300);
pair<string, int> delivery4;
delivery4.first = "Workshop";
delivery4.second = 400;
delivery4 = make_pair(string("Workshop"), 400);
pair<string, int> delivery5;
delivery5.first = "Smithy";
delivery5.second = 500;
delivery5 = make_pair(string("Smithy"), 500);
//Now make vector of these pairs
vector< pair<string, int> > deliveryJobs;
deliveryJobs.push_back(delivery1); // Should these specify pair's STRING and INT? If so, what's the syntax?
deliveryJobs.push_back(delivery2);
deliveryJobs.push_back(delivery3);
deliveryJobs.push_back(delivery4);
deliveryJobs.push_back(delivery5);
// This attempts to determine whether to display each of the five categories, and also attempts to multiply the Pair's INT with a random number
cout << "These Delivery Jobs are available...\n\n";
for (i = 0; i < deliveryJobs.size(); ++i) { // hopefully incrementing *after* the FOR loop runs
int ifListed = rand() % 2; // trying to get a 0 or 1 to use in an IF statement
if (ifListed != 0) {
jobsProfit = (rand() % 3) + 3; // trying to create a random set of numbers 3 through 5
deliveryJobs[i].second = jobsPay; // trying to call, through the vector, the INT from PAIR Delivery[i].second
jobsPay = (jobsPay + jobsProfit);
}
cout << i+1 << ") " << deliveryJobs[i].first << " : " << jobsPay << endl;
}
return 0;
}
|
>>> RESULT WHEN PROGRAMME RUNS:
These Delivery Jobs are available...
1) Clothiers : 4
2) Greenhouse : 9
3) Factory : 9
4) Workshop : 9
5) Smithy : 9
<<< END
...and it's always the same read-out. I'm hoping to get some of these categories randomly omitted. I'm also trying to access the Pairs' INTs e.g. 100, 200, and multiply those by a random number 3 through 5.
I've toyed with various options for several hours and it's been lots of fun but I've been staring at it too long.
I might be overlooking the declaration of certain variables, or declaring variables incompletely...
I probably have one or two lines of code in the wrong place *and* slightly out-of-whack, so I'm not managing to consider all the options when I re-try code.
Thanks in advance for taking the time to point-out errors.