Comparing array of string

Hi there!
I'm new so hello everyone, I need help with two pieces of my code, which I can't solve at all. Tried different combinations, but I always end up getting an error.

In short, what I'm trying to do is to compare months in Date1 and Date2. I'm passing two months (e.g January and February) and I return them. After that, there is a message saying either month in Date1 is correct/incorrect. (same for Date2)

... at least I WISH it worked that way. It works perfectly fine for days and years (as there is not array...)

Is there any way I can make it work like it is with "days" and "years"? Format should be like:

Month in Date1 is correct/incorrect. (depending on what you put it)
Month in Date2 is correct/incorrect.

The next thing is, comparing the dates. What's the best way to compare elements of classes? As in, say I have 21st March 2014 and 22nd April 2014. I want to do something like: "Date2 comes after Date1" but I don't really know how to get around with it. You'll see my initial attempts at the end of the code.

I'm adding the main() file and header file too, just to be clear.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include "Date.h"
using namespace std;

int main()
{
	date dates[3];
	string name_month[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
	bool found = false;
	char answer;
	int date_number[]={1,2,3};

	
	cout<<endl<<endl;
	
		for (int i=0; i<2; i++)
		{
		
		dates[i].change_time();
		}
		
		cout<<endl;

	
	for (int i=0; i<2; i++)
	{
	cout<<"Day in Date"<< date_number[i]<<" is: "<< dates[i].return_day();
	cout<<"\nMonth in Date"<< date_number[i]<<" is: "<< dates[i].return_month();
	cout<<"\nYear in Date"<< date_number[i]<<" is: "<< dates[i].return_year();
	cout<<endl<<endl<<endl<<endl;
	}
	
	for (int i=0; i<2; i++)
	{
		
	if (dates[i].return_day()<32)
	{
	cout<<"The day in Date"<< date_number[i]<<" is correct."<<endl;
	}
	
	else 
	{
	cout<<"\n\nThe day in Date"<< date_number[i]<<" is not correct."<<endl;
	}
    }
	
	
	for (int i=0; i<2; i++)
	{
	
	while (found==false)
		{	
			int s=0;
			//for (int i=0; i<2; i++)
	        //{
			while(s<12)
			{		
					//int a;
				//	for (int i=0; i<2; i++)
				//{	
					
					if (dates[i].return_month()==name_month[s])
				{
					found=true;
					s=13;
					cout<<"The month in date"<< date_number[i]<<" is correct."<<endl;
				}
				
				else if (dates[i].return_month()!=name_month[s])
				{
					s++;
				}
			
					if(s>0 && s!=13)
					cout<<"Month in date"<< date_number[i]<<" is not correct."<<endl;
                
		
		}
    }
}*/


for (int i=0; i<2; i++)
		{
		if (dates[i].return_year()>2020 || dates[i].return_year()<0)
		
		
		cout<<"Year in Date"<< date_number[i]<<" is not correct."<<endl;
		
		else
		
		cout<<"Year in Date"<<date_number[i]<<" is correct."<<endl;
		}
					
	


cout<<"Do you want to compare the dates? Y/N"<<endl<<endl;
cin>>answer;

if (answer=='Y' || answer=='y')

dates[1].compare_times(date dates[3]);

system("pause")
}














.... and header file:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

class date

{
	private:
		
		int year, day;
		string month;
		
		
	
	public:
		
	date();
		
	void change_time();
			
	int return_day();
	string return_month();
	int return_year();
	
	void compare_dates(date dates[3]);
	
	//void print_display();
};


	date::date()
	
{
	year=0;
	day=0;
	month="January";
	cout<<"Date is initially set to:"<<endl;
	cout<<day<<"/"<<month<<"/"<<year<<endl;
}


void date::change_time()
{

	cout<<"Change the day to:"<<endl;
	cin>>day;
	cout<<"Change the month to:"<<endl;
	cin.ignore();
	getline(cin, month);
	cout<<"Change the year to:"<<endl;
	cin>>year;
	
}

int date::return_day()

{
	return (day);
}

string date::return_month()

{
	return (month);
}

int date::return_year()

{
	return (year);
}

void date::compare_dates(date dates[3])
{
	if (dates[0]==dates[1] && dates[0]==dates[2]
	
	cout<<"The dates are the same"<<endl;
}




Thanks very much for any sort of help!
Any ideas?
Refreshed.
You could try storing the month as an int, as you have year and day. When you want to print the month, use a function to fetch the string version for that month.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string getMonthName(const int monthNum)
{
    //initial idx is empty so that the indices line up with the month they're commonly
    //associated with, i.e. monthNames[1] = "Jan", monthNames[7] = "Jul", etc...
    static std::string monthNames[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 

    //if the caller of this function has supplied an
    //argument that's beyond our bounds...
    if(monthNum < 1 || monthNum > 12)
    {
        return ""; //or throw an exception or something...
    }

    return monthNames[monthNum];
}
Topic archived. No new replies allowed.