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