I am extremely new to all things Computer Science, and I am currently taking a course on C . Right now I have a lab that involves using a class to print out a certain day of the week and to change that date by a certain amount. Everything in the code works, but for some reason, it only uses the data from the first time that I ran it, and it does not update regardless of what argument values I change. Any help would be greatly appreciated.
Main Code
#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
Weekday d1(5);
cout << "The current day is " << d1.to_name() << ".\n";
cout << "Tomorrow is " << d1.get_next() << ".\n";
cout << "Yesterday was " << d1.get_prev() << ".\n";
d1.change_by(3);
cout << "The current day is " << d1.to_name() << ".\n";
d1.change_by(-5);
cout << "The current day is " << d1.to_name() << ".\n";
}
Header File Code
#pragma once
#include <string>
using std::string;
class Weekday
{
private:
int day_num;
public:
Weekday(int n)
{
day_num = n;
}
string to_name()
{
string day;
if (day_num == 0)
day = "Sunday";
else if (day_num == 1)
day = "Monday";
else if (day_num == 2)
day = "Tuesday";
else if (day_num == 3)
day = "Wednesday";
else if (day_num == 4)
day = "Thursday";
else if (day_num == 5)
day = "Friday";
else if (day_num == 6)
day = "Saturday";
return day;
}
string get_next()
{
//return Weekday(day_num 1).to_name();
string next_day;
if (day_num == 0)
next_day = "Monday";
else if (day_num == 1)
next_day = "Tuesday";
else if (day_num == 2)
next_day = "Wednesday";
else if (day_num == 3)
next_day = "Thursday";
else if (day_num == 4)
next_day = "Friday";
else if (day_num == 5)
next_day = "Saturday";
else if (day_num == 6)
next_day = "Sunday";
return next_day;
}
string get_prev()
{
string prev_day;
if (day_num == 0)
prev_day = "Saturday";
else if (day_num == 1)
prev_day = "Sunday";
else if (day_num == 2)
prev_day = "Monday";
else if (day_num == 3)
prev_day = "Tuesday";
else if (day_num == 4)
prev_day = "Wednesday";
else if (day_num == 5)
prev_day = "Thursday";
else if (day_num == 6)
prev_day = "Friday";
return prev_day;
}
void change_by(int change)
{
day_num = change;
if (day_num > 7)
day_num = day_num % 7;
else if (day_num < 0)
day_num = day_num 7;
}
};
CodePudding user response:
I don't see anything wrong with the program.
Every time you run it, it's like if you were starting from scratch; so you initialize d1
to 5, you do all your computations, and you exit the execution. If you repeat it 10 times, you'll always get the same results. The operating system will load and unload your program with every execution. The state of variables such as d1
will be lost because the space reserved for those variables in the system's memory will be released.
If you want to test how d1
changes, you can wrap all your computations in a loop:
int main()
{
Weekday d1(5);
while (d1.to_name() != "Sunday")
{
cout << "The current day is " << d1.to_name() << ".\n";
cout << "Tomorrow is " << d1.get_next() << ".\n";
cout << "Yesterday was " << d1.get_prev() << ".\n";
d1.change_by(3);
cout << "Changing the current day 3 days ahead: " << d1.to_name() << ".\n";
d1.change_by(-5);
cout << "Changing the current day 5 days behind: " << d1.to_name() << ".\n\n";
}
}
// Outputs:
//
// The current day is Friday.
// Tomorrow is Saturday.
// Yesterday was Thursday.
// Changing the current day 3 days ahead: Monday.
// Changing the current day 5 days behind: Wednesday.
//
// The current day is Wednesday.
// Tomorrow is Thursday.
// Yesterday was Tuesday.
// Changing the current day 3 days ahead: Saturday.
// Changing the current day 5 days behind: Monday.
//
// The current day is Monday.
// Tomorrow is Tuesday.
// Yesterday was Sunday.
// Changing the current day 3 days ahead: Thursday.
// Changing the current day 5 days behind: Saturday.
//
// The current day is Saturday.
// Tomorrow is Sunday.
// Yesterday was Friday.
// Changing the current day 3 days ahead: Tuesday.
// Changing the current day 5 days behind: Thursday.
//
// The current day is Thursday.
// Tomorrow is Friday.
// Yesterday was Wednesday.
// Changing the current day 3 days ahead: .
// Changing the current day 5 days behind: Tuesday.
//
// The current day is Tuesday.
// Tomorrow is Wednesday.
// Yesterday was Monday.
// Changing the current day 3 days ahead: Friday.
// Changing the current day 5 days behind: Sunday.
CodePudding user response:
Recompile your code before running it again. To be extra sure that you're recompiling, delete the .exe file first.