the original problem is using two arrays type char then string, I am getting run off into the other array when referencing it by int, if the number goes over the size of the array what is before the other one it will slip into the next array and return its value. if the number is out of sync of that it blows up with a seg fault, I have no idea why.
now comes this problem, in trying to use something else to remove that first problem, I came across the vector thing in C++ ( and yes I do not know C++)
but using the examples in how to set up and use vector I am getting
examples I am using before I forget to put them in,
the last example on this page:
Array of Strings in C++ (3 Different Ways to Create) - GeeksforGeeks
and on this page
C++ Tutorial: A Beginner's Guide to std::vector, Part 1
Code:
date.cpp:55:1: error: ‘the_month’ does not name a type
the_month.push_back("dec");
for everything within the two arrays. I am getting error(s) like that.
here is my code:
Code:
#ifndef DATE_H
#define DATE_H
class TheDate
{
private:
int year;
int month;
int day;
void validate_dates(int dd, int mm, int yy);
public:
TheDate(int dd = 0, int mm = 0, int yy = 0000)
{
validate_dates(dd,mm,yy);
}
void setDay(int dd);
int getDay();
void setMonth(int mm);
int getMonth();
void setYear(int yy);
int getYear();
void get_Day_of_week(int day);
int get_is_leap_year(int year);
void get_what_month(int month);
};
#endif
cpp
Code:
#include <iostream>
#include <vector>
#include "date.h"
//using std::string;
using namespace std;
/*
const char* day_of_week[] = {
"Monday", "Tuesday", "Wensday", "Thursday", "Firday", "Saturday", "Sunday"
};
const char* The_Month[] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
const string day_of_week[] = {
"Monday", "Tuesday", "Wensday", "Thursday", "Firday", "Saturday", "Sunday"
};
const string The_Month[] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
*/
std::vector<string> the_day;
the_day.reserve(7);
the_day.push_back("monday");
the_day.push_back("tues");
the_day.push_back("wens");
the_day.push_back("thurs");
the_day.push_back("fri");
the_day.push_back("sat");
the_day.push_back("sun");
std::vector<string> the_month;
the_month.reserve(12);
the_month.push_back("Jan");
the_month.push_back("feb");
the_month.push_back("mar");
the_month.push_back("apr");
the_month.push_back("may");
the_month.push_back("jun");
the_month.push_back("july");
the_month.push_back("aug");
the_month.push_back("sep");
the_month.push_back("oct");
the_month.push_back("nov");
the_month.push_back("dec");
void TheDate::setDay(int dd)
{
day = dd;
}
int TheDate::getDay()
{
return day;
}
void TheDate::setMonth(int mm)
{
month = mm;
}
int TheDate::getMonth()
{
return TheDate::month;
}
void TheDate::setYear(int yy)
{
TheDate::year = yy;
}
int TheDate::getYear()
{
return TheDate::year;
}
void TheDate::get_Day_of_week(int day)
{
using std::cout;
using std::endl;
std::cout << " in get day of week day : " << day <<std::endl;
std::cout << the_day[day - 1] << std::endl;
}
int TheDate::get_is_leap_year(int year)
{
using std::cout;
using std::endl;
//set return values for bool.
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
std::cout << year << " is a leap year." << '\n';
return 1;
}
else
{
std::cout << year << " is not a leap year." << '\n';
return 0;
}
}
else
{
std::cout << year << " is a leap year." << '\n';
return 1;
}
}
else
{
std::cout << year << " is not a leap year." << '\n';
return 0;
}
// because it has to return something.
// well it should not even reach this actually.
return year;
}
void TheDate::get_what_month(int month)
{
using std::cout;
using std::endl;
std::cout << the_month[month - 1] << endl;
}
void TheDate::validate_dates(int dd, int mm, int yy)
{
int leap;
using std::cout;
using std::endl;
std::cout << "in validate dates" << '\n';
// make sure proper year other wise it all off anyways, or just cuz. ;)
if (yy <= 2000)
{
setYear(2001);
}
else
{
setYear(yy);
}
// Now find out if year is leap year or not.
// ensures proper year is being checked.
// by passing in getYear.
leap = get_is_leap_year(getYear());
//if true check for month is feb.
// if it is a leap year and feb check day
if (leap == 1 && mm == 2)
{
if (dd <= 0 || dd > 29)
{
setDay(1);
}
else
{
setDay(dd);
}
}
else
{
std::cout << " month is " << mm << std::endl;
//check for which months have same amount of ending days grouping case.
switch(mm)
{
case 2: // feb
if (dd <= 0 || dd > 28)
{
setDay(1);
setMonth(mm);
}
else
{
setDay(dd);
setMonth(mm);
}
break;
//31 days
case 1: //jan
case 3: //march
case 5: //may
case 7: //july
case 8: //augsut
case 10: //Oct
case 12: //dec
if ( dd <= 0 || dd > 31)
{
setDay(1);
setMonth(mm);
std::cout << " in 31 days months " << getDay() <<std::endl;
}
else
{
setDay(dd);
setMonth(mm);
std::cout << " dd = dd in 31 days day " << getDay() <<std::endl;
std::cout << dd << " day " << mm << " month " << std::endl;
get_Day_of_week(getDay() );
get_what_month(getMonth());
}
break;
//look for 30 days
case 4: // april
case 6: //june
case 9: //sept
if ( dd <= 0 || dd > 30)
{
setDay(1);
setMonth(mm);
}
else
{
setDay(dd);
setMonth(mm);
}
break;
default:
// if it gets here then the month has to then fall out of specs
// then day too is also invalid by default,
//so just set them both to a default day, month.
setMonth(1);
setDay(1);
break;
} // end switch
} // end else if
}
main
Code:
#include <iostream>
#include "date.h"
int main()
{
using namespace std;
int d,m,y;
std::cout << "enter day, month, year in number form" << std::endl;
std::cin >> d >> m >> y ;
TheDate trying(d,m,y);
/*
trying.setDay(d);
trying.setMonth(m);
trying.setYear(y);
*/
std::cout << '\n' << " in main " << '\n';
std::cout << '\n' << "day " << trying.getDay() << " month " << trying.getMonth() << " year " << trying.getYear() << "\n";
std::cout << std::endl;
trying.get_Day_of_week(trying.getDay());
trying.get_what_month(trying.getMonth());
trying.get_is_leap_year(trying.getYear());
std::cout << '\n' << "day " << trying.getDay() << " month " << trying.getMonth() << " year " << trying.getYear() << "\n";
return 0;
}
output when it worked, kind of
Code:
userx@/media/data/C-Projects/VSC/C++/class-date <> g++ -Wall date.cpp main.cpp
userx@/media/data/C-Projects/VSC/C++/class-date <> ./a.out
enter day, month, year in number form
12 3 2012
in validate dates
2012 is a leap year.
month is 3
dd = dd in 31 days day 12
12 day 3 month // day is 12 no 12 for day array so it goes to month array and
April // <-- returns this instead
March
in main
day 12 month 3 year 2012
April
day 12 month 3 year 2012
userx@/media/data/C-Projects/VSC/C++/class-date <>
if you look at the code and count the elements in day array then go to month array math says its jumping into that one to complete the job.
now with vectors it is just not working at all.
Code:
userx@/media/data/C-Projects/VSC/C++/class-date <> g++ -Wall date.cpp main.cpp
date.cpp:32:1: error: ‘the_day’ does not name a type
the_day.reserve(7);
^
date.cpp:33:1: error: ‘the_day’ does not name a type
the_day.push_back("monday");
^
.....
date.cpp:43:1: error: ‘the_month’ does not name a type
the_month.reserve(12);
^
date.cpp:44:1: error: ‘the_month’ does not name a type
the_month.push_back("Jan");
^
.....
but per example I have done what is needed, but I guess not due to the errors.
so now what?
PS yes I swiped this off someone else post in here trying to help him out then seen what I was getting, now my psyche is just not letting me let go of this until I figure out how to get it to work like it is suppose to.