So I'm trying to get my array to output 3 different elements from my array but seems to overwrite the previous 2 with the last one and then outputs the same thing 3 times.
After debugging, it seems to be that my last for loop has all elements in "objects" as intended but the parameters for SetWorkTask only hold the values for the last element.
I've been trying to figure this out for a while but with no success asking peers. This is my first year learning C++ and any help is greatly appreciated.
#include <iostream>
#include "WorkTask.h"
#include <stdexcept>
using namespace std;
int main() {
/* Array of at least 3 objects */
WorkTask objects[3];
int workTicketNumber;
string taskIdentifier;
int day;
int month;
int year;
string taskNotes;
/* Loop for input and output */
for (int tasks = 0; tasks < 3; tasks++) {
while (true) {
try {
cout << "Your Work Ticket Number: ";
cin >> workTicketNumber;
if (cin.fail() || workTicketNumber <= MIN) {
cout << "ERROR you must enter a number greater than 999.";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
throw std::invalid_argument("");
continue;
}
cout << "Your Task Identifier: ";
cin >> taskIdentifier;
cout << "The Due Date: \n";
cout << "Day: ";
cin >> day;
if (cin.fail() || day < 1 || day > 31) {
cout << "ERROR The day must be between 1 and 31.";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
throw std::invalid_argument("");
continue;
}
cout << "Month: ";
cin >> month;
if (cin.fail() || month < 1 || month > 12) {
cout << "ERROR The month must be between 1 and 12.";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
throw std::invalid_argument("");
continue;
}
cout << "Year: ";
cin >> year;
if (cin.fail() || year < 2000 || year > 2099) {
cout << "ERROR The year must be between 2000 and 2099.";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
throw std::invalid_argument("");
continue;
}
cout << "Your Task Notes: ";
cin >> taskNotes;
cout << "\n";
objects[tasks].SetWorkTask(workTicketNumber, taskIdentifier, day, month, year, taskNotes);
break;
}
//Invalid arugment exception
catch (invalid_argument invalid) {
cout << " Please enter a valid input..." << endl;
}
}
}
cout << "Here are your current WorkTasks: \n\n";
for (int tasks = 0; tasks < 3; tasks++) {
objects[tasks].ShowWorkTask(workTicketNumber, taskIdentifier, day, month, year, taskNotes);
}
return 0;
}
OUTPUT
Your Work Ticket Number: 3000
Your Task Identifier: adjw
The Due Date:
Day: 2
Month: 7
Year: 2001
Your Task Notes: $$$
Your Work Ticket Number: 5000
Your Task Identifier: afj
The Due Date:
Day: 4
Month: 8
Year: 2040
Your Task Notes: #####
Your Work Ticket Number: 7000
Your Task Identifier: sfk
The Due Date:
Day: 9
Month: 10
Year: 2080
Your Task Notes: %%%
Here are your current WorkTasks:
Work Ticket Number: 7000
Task Identifier: sfk
Due Date: 9-10-2080
Task Notes: %%%
Work Ticket Number: 7000
Task Identifier: sfk
Due Date: 9-10-2080
Task Notes: %%%
Work Ticket Number: 7000
Task Identifier: sfk
Due Date: 9-10-2080
Task Notes: %%%