Here are the details of the assignment:
I am to write a program with a class Worker that has 3 private data members (int age, int yrsService, and string jobTitle).
Then there is to be 2 objects of Worker type; Jones and Smith.
Data members initialized are as follows: 25, 3, Sales and 37, 10, President.
The displayed results should look like:
Age: 25
Service: 3
Job: Sales
Age: 37
Service: 10
Job: President
Age: 26
Service: 4
Job: Sales
Age: 38
Service: 11
Job: President
Age: 27
Service: 5
Job: Sales
Age: 39
Service: 12
Job: President
I'm not sure where to go from this point. Is overloading unary operators basically just a counter increment?
alright.... here is what I have now.... but I'm getting a bunch of errors... (I'm figuring that I'm overloading incorrectly for one) but "Worker" no appropriate default constructor available. Not sure what that is all about... errors are on lines 55, 62 (multiple errors), 71 (multiple errors), and line 64. here is my code.
You don't need the set() and get() accessors. In fact, I think the point of this assignment is to modify the values only through the constructor and ++ operator.
You had the right idea with your first post. Go back to that. Add the constructor, it's fine.
Now you also need to add the ++ operator:
1 2 3 4
Worker &operator++()
{
// add code here
}
With these changes you can define a Worker like this: Worker dave(51, 29, "Software Architect");
print out his stats like this: dave.printData();
and add a year of service like this: ++dave;
Wow! LOL.... yeah... you're correct. That made it work perfectly. Thanks for all the help. I can't believe it took me this long to do this assignment. Again. I truly appreciate it. Here is the final working code to the exact specifications of what the output is supposed to look like:
This is true with the conditions to only loop two times. With your help, this project is now complete. I'm on to something else now. Populating an integer array from a .txt file, then doing a sequential search to let the user guess a number 1-500 with only 20 numbers (that have been pre-selected and wrote to the file already) and keeps looping until a number is guessed or -1 is entered. Its a radio station simulation and outputs the winning number, the location the number was found, and the number of callers. So far I've gotten the file to populate the array and my function to search the array for the number and return the location.
#include<iostream>
#include<fstream>
usingnamespace std;
int sequenSrch();
int main()
{
cout << "This program simulates a radio station that asks the caller to guess a number.\n";
cout << "The number is compared against a list of 20 numbers between 1 and 500 inclusive.\n";
cout << "The contest is held until a number has been matched or a value of - 1 is entered.\n";
cout << "A message is displayed containing the winning number, the location in the list of numbers,";
cout << " the number of calls made, and the amount of the prize.";
int num = 20;
int size = 0;
int first, x;
int* prizeArray = newint[num];
ifstream prizeList;
prizeList.open("prizeList.txt");
while (prizeList >> x)
{
if (size == num)
{
int* newPrizeArray = newint[2 * num];
for (first = 0; first < size; first++)
{
newPrizeArray[first] = prizeArray[first];
}
delete[] prizeArray;
prizeArray = newPrizeArray;
num *= 2;
}
prizeArray[size] = x;
size++;
}
prizeList.close();
return 0;
}
int sequenSrch(int newPrizeArray[20], int value)
{
int i = 1, location = 0;
bool found = false;
while (!found && i < newPrizeArray[20])
{
if (newPrizeArray[i] == value)
{
found = true;
location = i;
}
i++;
}
return location;
}
Why are you using a dynamically expanding array? The size is fixed and your other code (like sequenSrch) assumes the array is 20 items.
sequenSrch() isn't quite right. After finding the entry it increments i before breaking out of the loop. Also what should it return if the item isn't found? Right now it will return size. And the terminating condition isn't right: you're ending when i < newPrizeArray[20]. I think you want to end when i<20.
well originally I was not sure of how many integers were in the file, so I was having the program find the number of integers in the file and making the array of that size then storing them in the array (in the order that they are in the file... I already did a program that sorts them in ascending order).
should it not increment i before breaking out of the loop? I added...
1 2 3 4 5 6 7 8 9
int guess()
{
int guess;
cout << "Hello caller. What number between 1 and 500 are you guessing?\n";
cin >> guess;
return guess;
}
obviously to get the guess of the "caller"
I also added...
cout << guess() << " is not in the list. Call again.\n";
just before i++ in sequenSrch()...
I was thinking that the loop would return 0 if integer is not found... is it not doing this?