I'm trying to perform error checking on my initializer value for date, so that a day or month outside the acceptable range will stop the program from debbuging, but I'm getting a debug error even for the acceptable range. I don't know where the error is from, so i posted the code.
// ConsoleApplication56.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<stdexcept>
#include<array>
using namespace std;
class Date
{
public:
explicit Date(int mo, int da, int ye)
{
setDate( mo, da, ye);
}
void setDate(int &m, int &d, int &y)
{
setMonth(m);
setDay(d);
setYear(y);
}
void setMonth(int &m)
{
if (month>0 && month <13)
{
month = m;
}else
throw invalid_argument("month must be 1-12");
}
unsigned int getMonth()const
{
return month;
}
void setDay(int &d)
{
if(day>0 && day<32)
{
day = d;
}else
throw invalid_argument("day must be 0-31");
}
unsigned int getDay()const
{
return day;
}
void setYear(int &y)
{
year = y;
}
unsigned int getYear()const
{
return year;
}
void print()
{
cout<< month <<'/' << day << '/' << year;
}
void nextDay()
{
int numberOfDaysToAdd = 1;
array <int,12> daysInAMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
cout << "the date before is: " << month << "/" << day << "/" << year << endl;
day += numberOfDaysToAdd;
while (day > daysInAMonth [month - 1 ] )
{
day-= daysInAMonth [month - 1 ];
month++;
if (month > 12){
month = 1;
year++;
}
}
cout << "the day after is: " << month << "/" << day << "/" << year << endl;
}
private:
unsigned int month;
unsigned int day;
unsigned int year;
};
int main()
{
char response = 'y';
Date date(12, 3, 2013 );
cout<<"The date is :";
date.print();
cout<<endl;
cout<<endl;
cout<<"do you wish to check the next date(y/n)? :";
cin >> response;
cout<<endl;
while(response == 'y')
{
date.nextDay();
cout<<endl;
cout<<"do you wish to check the next date(y/n)? : ";
cin >> response;
cout<<endl;
}
return 0;
}
You should check the m and d values not the month and day member variables.
void setMonth(int &m)
{
if (month>0 && month <13)
should be
void setMonth(int &m)
{
if (m>0 && m <13)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Okay so what this program is supposed to do is display the date in M/D/Y format and for some unknown reason it always displays 1/1/1. I have been skimming through this the past 40 mins and still can't understand where I went wrong. Please help me out. Needmorewordstopostthissssssssssssssssssssssssssssssssssssssssssssssss
#include <iostream>
using namespace std;
void displayDate();
class Date{
public:
int month;
int day;
int year;
int mmax;
//functions
int getMonth();
int getDay();
int getYear();
int setMonth();
int setDay();
int setYear();
};
int Date::setMonth(){
int i;
for (i=1; i!=0; i=1)
{
cout << "\n Input the month: ";
cin >> month;
if (month < 1) {
cout << "Please input a valid month";
}
else if (month > 12){
cout << "Please input a valid month";
}
else{
break;
}
}
return month;
}
int Date::setDay(){
int i;
for (i=1; i!=0; i=1)
{
cout << "\n Input the day: ";
cin >> day;
if (day < 1) {
cout << "Please input a valid day";
}
else if ((month == 1 || 3 || 5 || 7 || 8 || 10 || 12) && (day > 31)){
cout << "Please input a valid day";
}
else if ((month == 4 || 6 || 9 || 11) && (day > 30)){
cout << "Please input a valid day";
}
else if ((month == 2) && (day > 28)){
cout << "Please input a valid day";
}
else{
break;
}
}
return day;
}
int Date::setYear(){
int i;
for (i=1; i!=0; i=1)
{
cout << "\n Input the year: ";
cin >> year;
if (year < 1) {
cout << "Please input a valid year";
}
else if (year > 2021){
cout << "Please input a valid year";
}
else{
break;
}
}
return year;
}
int Date::getMonth(){
int month;
return month;
}
int Date::getDay(){
int day;
return day;
}
int Date::getYear(){
int year;
return year;
}
void displayDate(int d,int m, int y){
cout << "Date: " <<m;
cout << "/" <<d;
cout << "/" <<y;
}
main(){
Date x;
int m,d,y;
x.setMonth();
x.setDay();
x.setYear();
m = x.getMonth();
d = x.getDay();
y = x.getYear();
displayDate(d, m ,y);
}
In your code, the getter is not returning the class member variables rather the local variables delared.
int Date::getMonth(){
return month;
}
int Date::getDay(){
return day;
}
int Date::getYear(){
return year;
}
Make the getters like this to return the actual member value.
int Date::getYear(){
int year;
return year;
}
In here, you declare a new int year; variable, that is not initialized, then you return that variable instead of the member variable of your class.
Change it to
int Date::getYear(){
return year;
}
Similar for your other get() functions.
Your conditions are also not working as you want it to:
(month == 1 || 3 || 5 || 7 || 8 || 10 || 12)
doesn't compare month to all those numbers, the numbers just all evaluate to true. The condtion should be:
(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
I have tried to fix a number of problems in your code. And I have added comments explaining why:
#include <iostream>
// using namespace std; // avoid it if you can. It brings too many symbol in main workspace
using std::cout;
using std::cin;
void displayDate();
class Date{
private: // attributes should be private, only accessors should be public (encapsulation)
int month;
int day;
int year;
//int mmax; // comment out member that the code does not (still) use
//functions
public:
int getMonth();
int getDay();
int getYear();
int readMonth(); // setMonth should be a setter: int setMonth(int month) { this->month = month;}
int readDay();
int readYear();
};
int Date::readMonth(){
for (;;) // idiomatic form for an endless loop
{
cout << "\n Input the month: ";
cin >> month;
if (month < 1) {
cout << "Please input a valid month";
}
else if (month > 12){
cout << "Please input a valid month";
}
else{
break;
}
}
return month;
}
int Date::readDay(){
for (;;)
{
cout << "\n Input the day: ";
cin >> day;
if (day < 1) {
cout << "Please input a valid day";
}
else if (day > 31){ // other conditions were useless
cout << "Please input a valid day";
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30)){ // fix
cout << "Please input a valid day";
}
else if ((month == 2) && (day > 28)){
cout << "Please input a valid day";
}
else{
break;
}
}
return day;
}
int Date::readYear(){
for (;;)
{
cout << "\n Input the year: ";
cin >> year;
if (year < 1) {
cout << "Please input a valid year";
}
else if (year > 2021){
cout << "Please input a valid year";
}
else{
break;
}
}
return year;
}
int Date::getMonth(){
//int month; // the local variable would hide the member
return month;
}
int Date::getDay(){
//int day;
return day;
}
int Date::getYear(){
//int year;
return year;
}
void displayDate(int d,int m, int y){
cout << "Date: " <<m;
cout << "/" <<d;
cout << "/" <<y;
cout << '\n'; // do not forget end of lines
}
int main(){ // main shall be declared to return int
Date x;
int m,d,y;
x.readMonth();
x.readDay();
x.readYear();
m = x.getMonth();
d = x.getDay();
y = x.getYear();
displayDate(d, m ,y);
return 0; // better to be explicit
}
Other things could be improved, because re-usability is important in OOP. But I am too lazy to go further today ;-)
I have an issue compiling and running the code on visual studios. It gives me the error code "declaration is incompatible with" so and so. More specifically, when calling on the header in the code in the beginning of each function from the main CPP. I've tried changing the void to int in the header, but it didn't seem to fix it. I'm stuck now and need some guidance.
The error is:
"Severity Code Description Project File Line Suppression State
Error (active) E0147 declaration is incompatible with "void convertTime::invalidHr(int hour)" (declared at line 9 of "Time.h") Source.cpp 6 "
#include <iostream>
#include "Time.h"
using namespace std;
int convertTime::invalidHr(int hour) *//error on this line*
{
int convertHour = hour;
try
{
if (hour < 13 && hour > 0)
{
hour = hour + 12;
return hour;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input hour again in correct 12 hour format: ";
cin >> hour;
invalidHr(hour);
throw 10;
}
}
catch (int c) { cout << "Invalid hour input"; }
}
int convertTime::invalidMin(int min) *//error here*
{
int convertMin = min;
try
{
if (min < 60 && min > 0)
{
return min;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input minutes again in correct 12 hour format: ";
cin >> min;
invalidMin(min);
throw 20;
return 0;
}
}
catch (int e) { cout << "Invalid minute input" << endl; }
}
int convertTime::invalidSec(int sec) *//error here*
{
int convertSec = sec;
try
{
if (sec < 60 && sec > 0)
{
return sec;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input seconds again in correct 12 hour format: ";
cin >> sec;
invalidSec(sec);
throw 30;
return 0;
}
}
catch (int t) { cout << "Invalid second input" << endl; }
}
void convertTime::printMilTime()
{
cout << "Converted time: " << hour << ":" << min << ":" << sec;
}
And here is my header:
class convertTime
{
public:
int hour, min, sec;
void invalidHr(int hour);
void invalidMin(int min);
void invalidSec(int sec);
void printMilTime();
};
The return types of the member functions don't match.
In the class definition, you have:
void invalidHr(int hour);
void invalidMin(int min);
void invalidSec(int sec);
In the implementations, you have:
int convertTime::invalidHr(int hour) { ... }
int convertTime::invalidMin(int min) { ... }
int convertTime::invalidSec(int sec) { ... }
You need to change one of them so they match.
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)
{
const int MAX_DAY = 31; // setting max day to 31
const int MAX_MONTH = 12; // max month to 12
const int MIN_YEAR = 2000; // the min year 2000 as specified
const int MAX_YEAR = 2099; // max year 2099 as speciified
bool valid = true;
// setting the limits
if(ticketNumber < 0 || month < 1 || month > MAX_MONTH ||
day < 1 || day > MAX_DAY ||
year < MIN_YEAR || year > MAX_YEAR)
valid = false;
else if (clientId.length() < 1 || description.length() < 1)
valid = false;
else
{
myTicketNumber = ticketNumber;
myClientId = clientId;
myDay = day;
myMonth = month;
myYear = year;
myDescription = description;
}
return valid;
}
// 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: ");
}
}
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.
2) The variable tickets is an array and therefore in the for loop tickets[i] is of type WorkTicket.
3) SetDay is a member function of WorkTicket and takes int as argument.
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.
/* Program Name: BadDate.cpp
Function: This program determines if a date entered by the user is valid.
Input: Interactive
Output: Valid date is printed or user is alerted that an invalid date was entered
*/
#include <iostream>
bool validateDate(int, int, int);
using namespace std;
int main()
{
// Declare variables
int year;
int month;
int day;
const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
bool validDate = true;
This is the work of the housekeeping() method
Get the year, then the month, then the day
int housekeeping() {
cout << "Please enter a year: "<< endl;
cin >> year;
cout << "Please enter a month: "<< endl;
cin >> month;
cout << "Please enter a day: "<< endl;
cin >> day;
}
This is the work of the detailLoop() method
Check to be sure date is valid
int detailLoop() {
if(year <= MIN_YEAR) // invalid year
validDate = false;
else if (month < MIN_MONTH || month > MAX_MONTH) // invalid month
validDate = false;
else if (day < MIN_DAY || day > MAX_DAY) // invalid day
validDate = false;
}
This is the work of the endOfJob() method
test to see if date is valid and output date and whether it is valid or not
int endOfJob()
{
if(validDate == true)
{
// Output statement
cout << " Your date is valid!"<< endl;
}
else
{
// Output statement
cout << " Your date is not valid!"<< endl;
}
}
} // end of main() function
I have tried a lot of different solutions to this problem and I just can't seem to figure out why the compiler continues to give me this error in my header file. If somebody could please give me some insight, that would be very much appreciated. EDIT: Sorry forgot which line is giving the error. It's in the header file the line: Date(string mstr, int dd, int yy);
And yes I know the this = new Date... is a bad solution, I'm just working it out a bit ;)
Header:
#include <string>
#ifndef DATE_H
#define DATE_H
class Date{
public:
Date(int mm, int dd, int yy);
Date(string mstr, int dd, int yy);
void print();
void printFullDate();
void prompt();
void setMonth(int);
void setDay(int);
void setYear(int);
int getMonth();
int getDay();
int getYear();
static const int monthsPerYear = 12;
private:
int month;
int day;
int year;
int checkDay(int);
};
#endif
And here is the implementation if you need it(It's not totally finished, I'm just trying to test out some of the functions I've written):
#include <iostream>
#include <stdexcept>
#include "Date.h"
using namespace std;
Date::Date(int mm, int dd, int yy){
setMonth(mm);
setYear(yy);
setDay(dd);
}
Date::Date(string mstr, int dd, int yy){
cout << "It's working";
}
int Date::getDay(){
return day;
}
int Date::getMonth(){
return month;
}
int Date::getYear(){
return year;
}
void Date::setDay( int dd ){
day = checkDay(dd);
}
void Date::setMonth( int mm ){
if( mm > 0 && mm <= monthsPerYear)
month = mm;
else
throw invalid_argument("month must be 1-12");
}
void Date::setYear( int yy ){
year = yy;
}
int Date::checkDay( int testDay){
static const int daysPerMonth[ monthsPerYear + 1 ] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if( testDay > 0 && testDay <= daysPerMonth[ getMonth() ])
return testDay;
if( getMonth() == 2 && testDay == 29 && (getYear() % 400 == 0 || ( getYear() % 4 == 0 && getYear() % 100 != 0 ) ) )
return testDay;
throw invalid_argument("Invalid day for current month and year");
}
void Date::print(){
}
void Date::printFullDate(){
}
void Date::prompt(){
int userChoice = 1;
int mm, dd, yy;
string monthStr;
while(userChoice != 3){
cout << "Enter 1 for format: MM/DD/YYYY" << endl;
cout << "Enter 2 for format: Month DD, YYYY" << endl;
cout << "Enter 3 to exit" << endl;
cout << "Choice: " << endl;
cin >> userChoice;
while(userChoice < 1 || userChoice > 3){
cout << "Please enter a number 1 - 3 for the formats above." << endl;
cout << "Choice: " << endl;
cin >> userChoice;
}
if(userChoice != 3){
switch(userChoice){
case 1:
cout << "Enter Month (1 - 12): ";
cin >> mm;
setMonth(mm);
cout << "Enter Day of Month: ";
cin >> dd;
setDay(dd);
cout << "Enter Year: ";
cin >> yy;
setYear(yy);
break;
case 2:
cout << "Enter Month Name: ";
cin >> monthStr;
cout << "Enter Day of Month: ";
cin >> dd;
cout << "Enter Year: ";
cin >> yy;
this = new Date(monthStr, dd, yy);
break;
default:
break;
}
}
}
}
Problem #1: Add an include directive for string
#include <string>
Problem #2: Use the fully qualified std::string rather than just string, or place a using-declaration before your class definition:
using std::string;
Problem #3: You cannot reassign the this pointer:
this = new Date(monthStr, dd, yy); // ERROR!
What you are trying to do should be probably re-written as:
*this = Date(monthStr, dd, yy);
use std::string or declare using namespace std; at the beginning of the code.