Problem with function not returning value

Pages: 12
I still think Vlad's original suggestion was the simplest. Though there was I think a small bug, the month numbers go from 1 to 12, but the array subscript goes from 0 to 11.

I slightly changed the function, but it was derived from the earlier version. Mine is not better, (apart from one line). But it may possibly be simpler to read for a beginner.
string convertMonthNumber( int monthNumber )
    const string months[] = {

    if ((monthNumber < 1) || (monthNumber > 12 ))
        return "";
        return months[monthNumber - 1];  // Note: array subscript is 0 to 11
Take a step back, take a deep breath and relax. Open up notepad or wordpad. Write down, step by step, what you want your program to do. You don't have to be super detailed and you don't have to follow C++ syntax, just write it in plain old english. Once you do that, look it over, make sure it sounds right, then post it here. (The idea being, to help us and you get a solid handle on what we're trying to accomplish!)

This can also be helpful for yourself if you're stuck in a situation and don't know how to start or proceed. By breaking it down into steps you can start to see how you might approach each step. And once you know how you might approach each step, you can help those steps work together in a logical way.
Last edited on

Thanks. I had seen that he uses index starting from 1 at the last moment so I forgot to change expression inside square braces.:)
Took your advice Moeljbcp: here you go.

Program to print out renewal and cancellation notices to magazine subscribers:

Short summary: read subscriber's name, subscription start month and year, and
number of years subscribed. Will then output subscription status. Will loop
until user decides to exit.

month input function:

(reusing code from previous assignment, the switch statement to find the month)
Change from printing month to assigning month a number (1-2, based on month).
Must have one input parameter.
If user enters invalid month, function should issue error message until valid month
is entered.

year input function:
has a prompt string, a low and high year limit (not before 2012 and not beyong 2017)
error check and loop until correct year is entered

convert month number to month name function:
take month number assigned by month input fuction and returns the month name

current date function:
values for the current month and current year should be read from the user only
once (not every time the program loops to run another subscription)

name function
Read the subscriber's first name and then separately read the subscriber's last

subscription length function
use previous year and month input functions to determing month and year
the subscription started
must error check to ensure not more than 5 years prior to current year, and start
date must not be beyond current year
subscription month should be beyond current month
must pass argument to month input function to limit upper value of month user
can enter; error check and loop until valid month is entered
read length in whole years of subscription. error check that lenght is
between 1 - 5 years.

output fuction
outputs current date, subscriber's name, subscription start and expiration date
-expiration date one month before starting month
print account status:
-if current month is either expiration month or before expiration month:
print message asking subscriber to renew.
-if expiration date has passed, print message that subscription has been cancelled

ask user if they want to process another subscription. should loop until
user indicates they don't want to anymore.
-current date will remain same while program is executing.
Okay, that's an excellent start. You now have a good idea of what you'll be wanting to do. For basic program flow it'd go something like this:

Output information about the program. ("Hi, this is a subscription information program [..]" or what not)

Ask for the current date. (Year, then month)

Begin a loop that will continue until the user no longer wishes to continue.

Ask for information about when they began their subscription (Year, then month)
Ask for how long they subscribed (Years)

Output subscription status

Ask if they'd like to do it again. If they do, go back to the start of the loop. If not, exit out of the loop.

Program done!

Using that idea, if you can accomplish each one of those tasks individually, you should be able to get a good start.

Treat each one of these problems separately, and start making up your main() program flow, making use of the functions you already have written to help you. Once you have it written how you (more or less) want it to go from beginning to end, in C++, I will happily provide further assistance to fix specific problems. :)
Last edited on
Exactly. :) Now I just need to figure out why it's not ouputting the month like I want it. :P
Square one eh? With the function posted on the very first post of this page you would output the month like this:

cout << "The 1st month of the year is: " << convertMonthNumber(1);

You should be able to substitute the 1 for any other number and get the appropriate month name, or substitute the number for a variable and use that instead, too.
I'm trying to have it so that the program will output the correct month without me having to hard-code the corresponding month-number in it. Does that make sense?
Do you mean that you want to get the current (irl) date? I'd look into using ctime for that.
No no no. It's supposed to output the the month that the user inputs the initial letters for at the beginning of the program.
int input = 0;
char a,b,c; // Useless variables to be compatible with your function declaration.
getMonth(a,b,c, input);
cout << "The month you picked is: " << convertMonthNumber(input);

Hope that helps

I think so. The function that is supposed to output the actual month name is the one I've been having problems with. It's supposed to take the month number from the monthNumber function, pass it to convertMonthNumber, and from convertMonthNumber be able to output the name of the month.

Does that clear it up?
I'm going to have a separate function that outputs both the month and year together, something like this:


void currentDate(string& month, int& yearNumber) 
     cout << endl;
     cout << "Current Date: " << (this is where the month would go) 
              << " " << yearNumber << endl;
So I figured out the problem; when I declared monthNumber statements inside the switch as int's, the function made a separate version of monthNumber, and the value wasn't being passed via reference parameters.

Now I'm trying to make the function with the switch loop so that if the user inputs an invalid month, it'll loop and keep prompting until a correct month is entered.
Topic archived. No new replies allowed.
Pages: 12