Help! C++ assignment. The output should be format 1: 12/25/2009 format 2: Decemb
ID: 3679718 • Letter: H
Question
Help! C++ assignment. The output should be format 1: 12/25/2009
format 2: December 25, 2009
format 3: 20 December 2009
For some reason, it won't show the string month in my output.
Header File
**************************************************************
#include <iostream>
#include <string>
using namespace std;
class Date
{
private:
int day, month, year;
public:
void setMonth(int);
void setDay(int);
void setYear(int);
int getDay();
string getMonth(int);
int getYear();
void showFormat1();
void showFormat2();
void showFormat3();
};
DateClass.cpp
*******************************************************************************
#include <iostream>
#include "Date.h"
// setter
void Date::setMonth(int m){
month=m;
}
void Date::setDay(int d){
day = d;
}
void Date::setYear(int y){
year = y;
}
// getter
int Date::getDay()
{
return day;
}
string Date::getMonth(int m){
string month;
if (m=1)
month = "January";
else if(m=2)
month = "February";
else if (m=3)
month = "March";
else if (m=4)
month = "April";
else if (m=5)
month = "May";
else if(m=6)
month = "June";
else if (m=7)
month = "July";
else if (m=8)
month = "August";
else if (m=9)
month = "September";
else if(m=10)
month = "October";
else if (m=11)
month = "November";
else if (m=12)
month = "December";
else
month = "Invalid input";
return month;
}
int Date::getYear(){
return year;
};
void Date::showFormat1(){
cout << "The date format 1: " << month <<"/" << day <<"/" << year <<endl;
}
void Date::showFormat2(){
string month;
cout << "The date format 2: " << month <<" " << day <<", " << year << endl;
}
void Date::showFormat3(){
string month;
cout << "The date format 3: " << day <<" " << month <<" " << year << endl;
}
Main.cpp
*******************************************************************
#include <iostream>
#include <string>
#include "Date.h"
using namespace std;
int main()
{
Date dateInput;
int day1,month1,year1;
cout << "Enter the day of the date: " <<endl;
cin>> day1;
cout << "Enter the month of the month: " <<endl;
cin>> month1;
cout << "Enter the year of the year: " <<endl;
cin>> year1;
dateInput.setDay(day1);
dateInput.setMonth(month1);
dateInput.setYear(year1);
dateInput.showFormat1();
dateInput.showFormat2();
dateInput.showFormat3();
system("pause");
return 0;
}
Explanation / Answer
just remove statement
string month;
form string Date::getMonth(int m) method. Due to this the local variable is getting set to the string month and not the class variable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.