Can't seem to call string in instance

Oct 29, 2019 at 8:50pm
I am very new to C++, I have been struggling with functions calling from classes. I have been working on a very basic calendar program and for every error I fix another appears. they seem to be focused on calling of the printMonth function into the instance. I keep getting object errors. does anyone have an idea why I am getting this error. (switch edited down for space) I also may have included too much as its my first post and I'm trying to provide as much as possible.

1
2
3
4
5
6
7
8
9
10
11
#include "dateclass.h";
using namespace std;

int main()
{
		cout << "Enter month ", cin >> month;
		cout << "\nEnter Day of Birth ", cin >> day;
		cout << "\nEnter Year ", cin >> year;
		
		dateclass instance(month, day, year);
		instance.printMonth();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

static int month, day, year;
static int m, d, y;
string monthName = " ";

class dateclass
{
private:
public:
	dateclass(int month, int day, int year)
	{ }

	string printMonth()
	{
		return monthName;
}};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "dateclass.h"
using namespace std;
string printMonth(int month)
{
	int x{ 0 };
	x = month;
	switch (x)
	{
	case '01': case '1':
		monthName = "January";
					
	case '12':
		monthName = "December";
		
	default:
		cout << "\nInvalid Selection\n\n";
		
		return monthName;
}}		
Last edited on Oct 29, 2019 at 8:51pm
Oct 29, 2019 at 9:36pm
What is with all of those global variables? They probably should be part to the class and your constructor should initialize those class variables.



Oct 31, 2019 at 4:15am
would that prevent the return monthName?
Oct 31, 2019 at 9:05am
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
#include <iostream>
#include <string>
using namespace std;

class dateclass
{
   int month, day, year;          // <==== NOT global; 
   
public:
   dateclass( int m, int d, int y) : month(m), day(d), year(y) { }    // <==== constructor sets variables

   void printMonth();            // <==== if it doesn't return anything make it void
};

	
void dateclass::printMonth()
{
   const string names[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
   if ( month < 1 || month > 12 ) cout << "Invalid month\n";
   else                           cout << names[month-1];
}


int main()
{
   int month, day, year;   // <===== declare LOCAL variables here
   cout << "Enter month: ";   cin >> month;           // <===== comma operator not appropriate here
   cout << "Enter day: ";   cin >> day;
   cout << "Enter year: ";   cin >> year;
		
   dateclass instance(month, day, year);
   instance.printMonth();
}                          // <===== make sure you paste ALL of your code 


Enter month: 10
Enter day: 31
Enter year: 2019
October
Last edited on Oct 31, 2019 at 9:45am
Topic archived. No new replies allowed.