Calling a function for the first time (date)

I am writing a program that calls a function for the first time. The assignment is to write a program that prompts the user to enter a day, month, and year (as integers). The program then calls a function that returns a string representing the date in the format “Month Day, Year." I know how to prompt the user to enter the integers and I know how to print out the results but I am confused on calling a function. Is there a pre-known function out there that I am supposed to use or do I make this up completely? The code attached is how I started but I know I am not going in the correct direction.

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

#include <iostream>
#include <string>

using namespace std;

int date_return(string mm)
{
    int d;
    int m;
    int y;
    
    cout << "Please enter a day of the month, in integer form: ";
    cin >> d;
    
    cout << "Please enter a month, in integer form: ";
    cin >> m;
    
    cout << "Please enter a year, in integer form: ";
    cin >> y;


    if(m=1){
        mm = January;
    }
    else if(m=2){
        mm = February;
    }
    else if(m=3){
        mm = March;
    }
    else if(m=4){
        mm = April;
    }
    else if(m=5){
        mm = May;
    }
    else if(m=6){
        mm = June;
    }
    else if(m=7){
        mm = July;
    }
    else if(m=8){
        mm = August;
    }
    else if(m=9){
        mm = September;
    }
    else if(m=10){
        mm = October;
    }
    else if(m=11){
        mm = November;
    }
    else(m=12);{
        mm = December;
    }
    
    cout << "The date you entered is: " << mm << d << "," << y << endl;
    
    return 0;
}
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
#include <iostream>
#include <string>

// function that returns a string representing the date in the format “Month Day, Year."
// return type: std::string
// input: three integers representing the year, month (january=1), and day
// invariant: input contains valid values eg. month in [1,12]
std::string date_string( int year, int month, int day )
{
    // the tail part of the date string containing text representations of  day and year
    const std::string tail = std::to_string(day) + ", " + std::to_string(year) ;

    // return a string with the name of the month added as the prefix
    switch(month) // use a lookup table (an array) instead?
    {
        case 1 : return "January " + tail ;
        case 2 : return "February " + tail ;
        case 3 : return "March " + tail ;
        case 4 : return "April " + tail ;
        case 5 : return "May " + tail ;
        case 6 : return "June " + tail ;
        case 7 : return "July " + tail ;
        case 8 : return "August " + tail ;
        case 9 : return "September " + tail ;
        case 10 : return "October " + tail ;
        case 11 : return "November " + tail ;
        case 12 : return "December " + tail ;
        default : return "invalid month " + tail ;
    }
}

int main()
{
    std::cout << "enter date as three space separated integers yyyy mm dd: " ;
    int yyyy, mm, dd ;
    std::cin >> yyyy >> mm >> dd ;

    // pass the three integers yyyy, mm and dd as input the function
    // and print out the string that the function returns
    std::cout << date_string( yyyy, mm, dd ) << '\n' ;
}
@shimshim96:

You need to double-check all your if statements, and make sure you understand the difference between the assignment operator = and the equality comparison operator ==.
Registered users can post here. Sign in or register to post.