Testing a class using for loop in int main

Testing a class using for loop in int main



In my main I have to test the class i made with a for loop but am a little stuck. I have to loop through 3 times and get user input for the ticketing system. I tried making an array and storing the user input but nothing I have tried so far has worked. Im trying to call upon the SetWorkTicket with my loop.


#include<iostream>
#include<iomanip>
#include<stdexcept>
#include<sstream>


using namespace std;

// start of the WorkTicket class
class WorkTicket

public:

// defailt constructors, if the parameters are not specified the ticket will be set to 0 and the date to 1/1/2000. The rest empty strings

WorkTicket() : myTicketNumber(0), myClientId(""), myDay (1), myMonth (1), myYear(2000), myDescription ("")
WorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description);

// mutator method that sets all attributes of the object to the parameters as long as valid. If no problems are being detected set TRUE if not then FALSE.....
bool SetWorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description);

// accessor method that will show all the attributes of the ticket in the console menu
void ShowWorkTicket() const;

// here are the sets and gets for the attributes

// ticket number

void SetTicketNumber(int ticketNumber);
int GetTicketNumber() const return myTicketNumber;

// client id

void SetClientId(string clientId) myClientId = clientId;
string GetClientId() const return myClientId;

// date by day

void SetDay(int day);
int GetDay() const return myDay;

// date by month

void SetMonth(int month);
int GetMonth() const return myMonth;

// date by year

void SetYear(int year);
int GetYear() const return myYear;

// description

void SetDescription(string description) myDescription = description;
string GetDescription() const return myDescription;


private:

int myTicketNumber;
string myClientId;
int myDay;
int myMonth;
int myYear;
string myDescription;

;



// the work ticket constructor definition
WorkTicket::WorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description)


SetTicketNumber(ticketNumber);
SetClientId(clientId);
SetMonth(month);
SetDay(day);
SetYear(year);
SetDescription(description);


// set work ticket
bool WorkTicket::SetWorkTicket(int ticketNumber, string clientId, int month, int day, int year, string description)


// this is to show the work ticket, it will show everything in the console..
void WorkTicket::ShowWorkTicket() const

cout << "nWork Ticket Number: " << myTicketNumber
<< "nClient ID: " << myClientId
<< "nDate: " << setw(2) << setfill('0') << myMonth
<< "/" << setw(2) << setfill('0') << myDay
<< "/" << myYear
<< "nIssue: " << myDescription << endl;


void WorkTicket::SetTicketNumber(int ticketNumber)

if(ticketNumber > 0)

myTicketNumber = ticketNumber;

else

throw invalid_argument("Try Again.");



// WorkTicket::SetDay definition
void WorkTicket::SetDay(int day)

const int MIN_VALID = 1;
const int MAX_VALID = 31;
if(day >= MIN_VALID && day <= MAX_VALID)

myDay = day;

else

throw invalid_argument("Try Again.");



// WorkTicket::SetMonth definition
void WorkTicket::SetMonth(int month)

const int MIN_VALID = 1;
const int MAX_VALID = 12;
if(month >= MIN_VALID && month <= MAX_VALID)

myMonth = month;

else

throw invalid_argument("Try Again.");



// WorkTicket::SetYear definition
void WorkTicket::SetYear(int year)

const int MIN_VALID = 2000;
const int MAX_VALID = 2099;
if(year >= MIN_VALID && year <= MAX_VALID)

myYear = year;

else

throw invalid_argument("Try Again.");



int main()

const int NUMBER_OF_TICKETS = 3;
WorkTicket tickets[NUMBER_OF_TICKETS];

int ticketNumber;
string clientId;
int day;
int month;
int year;
string description;

for (int i = 0; i <NUMBER_OF_TICKETS; i++)

cout << "WorkTicket [" << i << "]: " << endl;

cout << "Enter the ticket number: " << endl;
cout << "Enter the client ID: " << endl;
getline(cin, clientId);
day = SetDay("Enter a day number: ");








There seems to be a lot of code unrleated to your problem. Please try to create a Minimal, Complete, and Verifiable example and post it.

– R Sahu
Sep 17 '18 at 3:15






Why is your SetDay function being passed a string when it takes an integer?

– GKE
Sep 17 '18 at 3:28


SetDay






Where do you use that WorkTicket array in main after you declared it? You have to actually do something with the array tickets at some point, but you failed to do anything with it.

– PaulMcKenzie
Sep 17 '18 at 3:30



WorkTicket


main


tickets




2 Answers
2



Gather the input and then construct a WorkTicket. Push the tickets into a vector. In the code yet to be written, the newly create WorkTickets can be tested for correctness.



Make sure to include vector at the top of the file.


int main()

const int NUMBER_OF_TICKETS = 3;

int ticketNumber;
string clientId;
int day;
int month;
int year;
string description;
std::vector<WorkTicket> tickets;

for (int i = 0; i <NUMBER_OF_TICKETS; i++)

cout << "WorkTicket [" << i << "]: " << endl;

cout << "Enter the ticket number: " << endl;
cin>> ticketNumber;
cout << "Enter the client ID: " << endl;
cin >> clientId;
cout << "Enter a day number: " << endl;
cin >> day;
cout << "Enter a month number: " << endl;
cin>> month;
cout << "Enter a year number: " << endl;
cin>> year;
cout << "Enter a description : " << endl;
cin >> description;
try
tickets.push_back(ticketNumber, clientId, month, day, year, description);

catch(std::exception e)

std::cout << e.what() << std::endl;





Assuming the rest of your code works correctly and you only have problems in the main:



There are a few issues as commented by others in your code in main:



1) You declared WorkTicket tickets[NUMBER_OF_TICKETS]; but have not used it anywhere.


WorkTicket tickets[NUMBER_OF_TICKETS];



2) The variable tickets is an array and therefore in the for loop tickets[i] is of type WorkTicket.


tickets


tickets[i]


WorkTicket



3) SetDay is a member function of WorkTicket and takes int as argument.


SetDay


WorkTicket


int



In light of these, following changes made:


for (int i = 0; i <NUMBER_OF_TICKETS; i++)

cout << "WorkTicket [" << i << "]: " << endl;

cout << "Enter the ticket number: " << endl;
cout << "Enter the client ID: " << endl;
getline(cin, clientId);
cout<<"Enter a day number:";
int day;
cin>>day;
tickets[i].SetDay(day); //day is int and tickets[i] is of WorkTicket




Hope this helps to understand the main part better.


main






A little explanation would be helpful, otherwise a cargo cult programmer is in the making.

– PaulMcKenzie
Sep 17 '18 at 3:32



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế