Hello Fellas I was working with a C++ programming book and I've been working through the chapters and i came across an assignment i decided to tackle. For some reason i keep having problems, I had been working on this assignment for a while and the reason i chose this assignment is because i've seen a lot of posts online talking about this assignment and I was wondering if you guys could explain to me what i am doing wrong. Thanks in advance.
Here is the Implementation File and what i'm trying to accomplish is this: creating a class that is dayType and be able to print the day, set the day, return the next and previous days and have constructors to be able to do so and then test it with the main method to show everything as usual.
#include <iostream>
usingnamespace std;
class dayType{ //dayType class
public:
dayType();
dayType(int d); //constructors
void setDay(int setd);
void printDay() const;
int getDay() const; //abstract methods
int nextDay();
int prevDay();
private:
int day; //private variable in class
};
dayType::dayType(){}
dayType::dayType(int d){ //constructor definitions
d=day;
}
void dayType::setDay(int setd){ //setDay definition
setd=day;
}
void dayType::printDay() const{ //printDay definition
if(day == 1){
cout << "The day is Sunday" << endl;
}
elseif( day == 2){
cout << "The day is Monday" << endl;
}
elseif(day == 3){
cout << "The day is Tuesday" << endl;
}
elseif(day == 4){
cout << "The day is Wednesday" << endl;
}
elseif(day == 5){
cout << "The day is Thursday" << endl;
}
elseif(day == 6){
cout << "The day is Friday" << endl;
}
elseif(day == 7){
cout << "The day is Saturday" << endl;
}
else{
cout << "Invalid Day" << endl;
}
}
int dayType::getDay() const{ //getDay definition
return day;
}
int dayType::nextDay(){ //nextDay Definition
day++;
if(day > 7){
day = 1;
}
return day;
}
int dayType::prevDay(){ //previous day definition
day--;
if(day < 1){
day = 7;
}
return day;
}
This is my main Method, every time i try to test the class with the main method i always get garbage some huge negative number which is probably just memory trash i'm assuming.
#include "dayType.h"
#include <iomanip>
#include <iostream>
usingnamespace std;
int main(){
dayType test(1); //making an object of class
test.setDay(1);
test.printDay(); //just trying to get it to print the correct output
cout << test.getDay(); //always prints garbage
system("pause");
return 0;
}
i've been experimenting with this forever and for some reason i can't get it to work right any help would be appreciated. Thanks.
Ha! very stupid mistake on my part. I should have known that! Thank you very much Guestgulkan for pointing that out for me i been banging my head for a while now cause i knew the rest was right but i was so puzzled why that wasn't working. I really appreciate your help :)