Problem with function not returning value

Pages: 12
I'm working on this programming assignment that has me creating a program that handles renewal and cancellation notices for magazine subscriptions, based on the month and year the user inputs. Part of the program focuses on reusing old code (which is what the extended switch is from). The function that I made with the reused code instead of displaying the actual month, equates the month with a number (1-12, correlating to each month). My issue is that I'm trying to make a separate function that takes the number and converts it the actual name of the month. This is what I have so far:

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>

using namespace std;

void getMonth(char first, char second, char third, int& monthNumber);
void getYear(int& yearNumber);
void convertMonthNumber(int monthNumber, string& month);

int main()
{
    char first, second, third;
    int monthNumber = 0;
    int yearNumber = 0;
    string month;    

    cout << "Subscription Evaluation Program";
    cout << endl << endl;
    getMonth(first, second, third, monthNumber);
    getYear(yearNumber);

    system("PAUSE");
}

void getMonth(char first, char second, char third, int& monthNumber)
{  
    cout << "Enter first letter of the current month: ";
    cin >> first;

switch(first)
{
    case 'F':
    case 'f':
        {
             int montNumber = 2;
        }
        break;
    case 'S':
    case 's':
         {
             int monthNumber = 9;
         }
        break;
    case 'O':
    case 'o':
        {
             int monthNumber = 10;
        }
        break;
    case 'N':
    case 'n':
        {
             int monthNumber = 11;
        }     
        break;
    case 'D':
    case 'd':
        {
             int monthNumber = 12;
        }
        break;
    case 'A':
    case 'a':
    {
        cout << "Enter second character of month: ";
        cin >> second;

        switch(second)
        {
            case 'P':
            case 'p':
                {
                     int monthNumber = 4;
                }
                break;
            case 'U':
            case 'u':
                {
                     int monthNumber = 8;
                }
                break;
            default:
                cout << "Unknown Month";
                cout << endl;
          }
    }
break;
case 'J':
case 'j':
{
    cout << "Enter second character of month : ";
    cin >> second;
    switch(second)
    {
        case 'A':
        case 'a':
            {
                 int monthNumber = 1;
            }
            break;
        case 'U':
        case 'u':
            cout<<"\nEnter third character: ";
            cin >> third;
            switch(third)
            {
                case 'L':
                case 'l':
                    {
                         int monthNumber = 7;
                    }    
                    break;
                case 'N':
                case 'n':
                    {
                         int monthNumber = 6;
                    }
                    break;
                default:
                    cout << "\nUnknown Month";
            }
            break;
        default:
            cout << "\nUnknown Month";
            cout << endl;
    }
    break;
    case 'M':
    case 'm':
        cout << "Enter second and third characters: ";
        cin >> second;
        cin >> third;
        switch(second)
        {
            case 'A':
            case 'a':
                {
                    switch(third)
                    {
                        case 'R':
                        case 'r':
                            {
                                 int monthNumber = 3;
                            }     
                            break;
                        case 'Y':
                        case 'y':
                            {
                                 int monthNumber = 5;
                            }    
                            break;
                        default:
                            cout << endl << "Unknown Month";
                            cout << endl;
                    }
                }
                break;
            default:
                cout << endl << "Unknown Month";
                cout << endl;
        }
    break;
    default:
        cout << endl << "Unknown Month";
        cout << endl;
        return;
    }
}

void getYear(int& yearNumber)
{
    const int LOW_YEAR_LIMIT = 2012;
    const int HIGH_YEAR_LIMIT = 2017;

    do{
    cout << "Enter current year (4 digits): ";
    cin >> yearNumber;
    if (yearNumber < LOW_YEAR_LIMIT || yearNumber >= HIGH_YEAR_LIMIT){
        cout << endl;
        cout << "Invalid year. Please enter again.";
        cout << endl << endl;
        }
    }while (yearNumber < LOW_YEAR_LIMIT || yearNumber >= HIGH_YEAR_LIMIT);
    return;
}

void convertMonthNumber(int monthNumber, string& month)
{  
    const string JANUARY = "January";
    const string FEBRUARY = "February";
    const string MARCH = "March";
    const string APRIL = "April";
    const string MAY = "May";
    const string JUNE = "June";
    const string JULY = "July";
    const string AUGUST = "August";
    const string SEPTEMBER = "September";
    const string OCTOBER = "October";
    const string NOVEMBER = "November";
    const string DECEMBER = "December";

    if (monthNumber == 1)
         month = JANUARY;
    else if (monthNumber == 2)
         month = FEBRUARY;
    else if (monthNumber == 3)
         month = MARCH;
    else if (monthNumber == 4)
         month = APRIL;
    else if (monthNumber == 5)
       month = MAY;
    else if (monthNumber == 6)
         month = JUNE;
    else if (monthNumber == 7)
         month = JULY;
    else if (monthNumber == 8)
         month = AUGUST;
    else if (monthNumber == 9)
         month = SEPTEMBER;
    else if (monthNumber == 10)
         month = OCTOBER;
    else if (monthNumber == 11)
         month = NOVEMBER;
    else if (monthNumber == 12)
         string month = DECEMBER;  
    return;
}
Your function has a logical error. This statement

1
2
   else if (monthNumber == 12)
         string month = DECEMBER;  


declares a local variable month that will be destroyed after that statement.


It is better to define the function the following way

1
2
3
4
5
6
7
8
std::string convertMonthNumber( int monthNumber )
{  
    const char *months = 
        { "January", "February", "March", "April", "May", "June",
           "July", "August", "September", "October", "November", "December" };

    return ( ( monthNumber < 1 || monthNumber > 12 ) ? "" : months[monthNumber] );   
}
Last edited on
Is there another way to do it than the one you suggested? Something a little more simple? I don't think we've gone over some of the things you used in my class. It's just that I'm not sure really what you did. Haha.
I showed the simplest way. What statement is hard to understand to you?
The *months part and the months[monthNumber] part. I don't know what they are and what they mean.

Is there a way to make my extended if-statements work?
Well if you do not want to use the better function realization then let update your function

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
void convertMonthNumber(int monthNumber, string& month)
{  
    const string JANUARY = "January";
    const string FEBRUARY = "February";
    const string MARCH = "March";
    const string APRIL = "April";
    const string MAY = "May";
    const string JUNE = "June";
    const string JULY = "July";
    const string AUGUST = "August";
    const string SEPTEMBER = "September";
    const string OCTOBER = "October";
    const string NOVEMBER = "November";
    const string DECEMBER = "December";

    if (monthNumber == 1)
         month = JANUARY;
    else if (monthNumber == 2)
         month = FEBRUARY;
    else if (monthNumber == 3)
         month = MARCH;
    else if (monthNumber == 4)
         month = APRIL;
    else if (monthNumber == 5)
       month = MAY;
    else if (monthNumber == 6)
         month = JUNE;
    else if (monthNumber == 7)
         month = JULY;
    else if (monthNumber == 8)
         month = AUGUST;
    else if (monthNumber == 9)
         month = SEPTEMBER;
    else if (monthNumber == 10)
         month = OCTOBER;
    else if (monthNumber == 11)
         month = NOVEMBER;
    else if (monthNumber == 12)
         month = DECEMBER;  
    else 
         month = "";  
}
Last edited on
Thanks for that. I made the changes, but it's still not outputting the month?
That epically huge switch statement to convert input to a month number made me cry. I tackled the problem to see what I could come up with. Can anybody do better on this one? I don't really like initializing a map every damn time the function is called but it seemed clever at the time.

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
#include <string>
#include <map>

using namespace std;

void ToUpper(string& in)
{
	unsigned i;
	for (i=0;i<in.length();i++)
	{
		if (in[i] >= 'a' && in[i] <= 'z')
			in[i] += 'A' - 'a';
	}
	return;
}

int getMonth()
{
	string input;
	map<string, int> Months;
	Months["JAN"] = 1; Months["FEB"] = 2; Months["MAR"] = 3; Months["APR"] = 4;
	Months["MAY"] = 5; Months["JUN"] = 6; Months["JUL"] = 7; Months["AUG"] = 8;
	Months["SEP"] = 9; Months["OCT"] = 10; Months ["NOV"] = 11; Months["DEC"] = 12;
	
	do
	{
		cout << "Enter at least the first three letters of the current month: ";
		cin >> input;
		
		if (input.length() > 3)
		{
		input = input.substr(0, 3);
		ToUpper(input);
		if (Months.count(input) > 0)
			return Months[input];
		}
		cout << "Invalid month name, please try again!\n";
	} while (1==1);
}
Last edited on
It may have made you cry, but that was an earlier program we wrote and were told to use the original code, and modify it. It's a beginning programming class, and I have no idea what you did.

I'm a noob, I'm not trying to do anything fancy or gucci. Haha.
@ComradeCookie
Thanks for that. I made the changes, but it's still not outputting the month?


First of all I do not see where you use the function. It can be called for example the following way


1
2
3
std::string month;
convertMonthNumber( 10 /* or other value */, month );
std::cout << "month = " << month << std::endl;
Last edited on
Don't worry about it, it wasn't meant to be an insult; I mostly just wanted to see if I could reduce it down to something more managable, for fun. :) I'm sorry it's a little off topic.

As for the code, it uses std::map, which is just a special kind of data type that can have a key value and a data value. In this case the key is a string, and the data value is an int. This way you can map "JAN" to "1" and so on, but in a way that you can take input and directly use it to try to find a value in the map that matches it.

1
2
3
4
5
6
map<string, int> Months;
Months["JAN"] = 1;

string blah = "JAN";
int i = Months[blah]; //valid, i = 1 now.
i = Months.count("Not defined in here"); // i = 0 now, because no value is mapped to that string. 


The ToUpper function just makes a string uppercase, admittedly in a slightly hard to understand way. But hey, does what it says on the label. The rest is just putting all these pieces together.
Last edited on
It's okay Moeljbcp. :P Just like I'm a bronzer in Starcraft 2, we all have to start somewhere.
To moscow, where do you get the 10?
I tried to explain some of the logic behind how it works, so it isn't just a big ol' chunk of code. Hopefully it at least helps broaden your view of what things might be possible for you in the future, and how you might accomplish them. :)

As for your problem, turn your attention back to the main() function and work in there. All I see in what you pasted is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    char first, second, third;
    int monthNumber = 0;
    int yearNumber = 0;
    string month;    

    cout << "Subscription Evaluation Program";
    cout << endl << endl;
    getMonth(first, second, third, monthNumber);
    getYear(yearNumber);

    system("PAUSE");
}


And never is the function convertMonthNumber() ever even called. What's the problem at this point?
Last edited on
I just didn't update it is all. Here's what it looks like (at least main)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    char first, second, third;
    int monthNumber = 0;
    int yearNumber = 0;
    string month;    
    
    cout << "Subscription Evaluation Program";
    cout << endl << endl;
    getMonth(first, second, third, monthNumber);
    getYear(yearNumber);
    convertMonthNumber(monthNumber, month);
    
    cout << endl;
    cout << "The current month is: " << month << endl;  
    system("PAUSE");
    return 0;
}
@ComradeCookie

Switch on your brain! I showed you how the function can be called.
@moscow I asked you where you got the 10 from. Why are you calling it like that?
You have some problem with your brain?!!! I showed you an example how the function can be called. And even in the comment for the function argument I wrote that you can use any value for the month number.

He just used the 10 as an arbitrary value to show its use (should make string equal to "October"). You can input any number 1-12 and then string will contain the correct name of that month. Use that function to help expand your main() further towards your goal :)
Last edited on
Maybe I'm just tired, and am just completely misunderstanding what's going on, but I'm trying to have it so it'll come up with the month automatically, without me having to hard-code the number into the function call to get the correct month displayed. Make sense?

Please bare with me. :(
Pages: 12