First of all, I'm 17 learning C++ on my own! I've been enjoying Daniweb. and I did like to thank the community for that! Thank you all!
Here goes my Problem...
I have a small program that calculates time between two instances
and it worked fine... so I decided to extend it by making a derived class of the base class 'Time' which will hold first, and second Input and the final result. other features later....
here is a screenshot of the first one...
... this one works just fine and nice... its fun. please take note of the ans//the last figures******** Here is a screenshot of the extended program
...the result of the time difference is weirdThe input was the same with the first one... And you can actually see that the first one is correct if you do the calculations... Or whats the time difference between, the inputs Time2 = 3hrs, 4mins, 6sec Time1 = 9mins, 8sec isn't it Time difference = 2hrs, 54mins, 58sec as shown in the first screenshot... but the second is weird and nothing was changed in the base class! The first Screenshot was a direct interaction with the base class 'Time' no derived at that time. #include "time.h" The second is purely using the derived class 'Timer'...#include "timer.h" Its a multifile program... but I copied only relevant stuff here Now here is a well documented code,truncated a lot to save space, but all required info are here ...Actual total lines is about 860
/////////////Time class/////////////////////////////////////
class Time{
private:
int yr; int mnth; int day;
int hr; int min; int sec;
short currentState; //state, whether calculation is good or weird
//0, if good i.e everything is ok
//1, if the time value is weird such as 63sec, 20 000years etc
protected:
bool order(const Time& t1, const Time& t2); //For checking subtraction
//order or which is greater
//.. of t1, and t2
public:
Time() : yr(0), mnth(0), day(0), hr(0), min(0), sec(0)
{ currentState = 0; } //Constructor, initializes everything to zero
Time(int y, int mn, int d, int h, int m, int s) :
yr(y), mnth(mn), day(d), hr(h), min(m), sec(s)
{ currentState = 0; } //Direct Call, Constructor 6 args
void getInput(); //gets user input according to the parameter passed
//by getTime
void getTime(); //Gets Time, Uses switch and calls getInput
//The rest of the Member Functions are Modifier Functions
void validate(); //Checks User input and
// modifies "currentState" appropriately
void display(); //For displaying output
Time operator - (Time& t2); //For calculating time difference
int State(){
return currentState;
}
};
////////////////////////////an Implementation///////////////////
Time Time::operator - (Time& t2){
bool state;
Time t1;
state = Time::order(*this, t2); //State will be true if t1 > t2
//in that case, subtract directly
//If state is false, Swap Time so that
//We can have t1 > t2
//THIS IS TO PREVENT NEGATIVE NUMBERS
//Check Order of Subtraction
if(state == false) timeSwap(*this, t2);
//calls a fuction that swaps their values
//Seconds
if(sec > t2.sec) t1.sec = sec - t2.sec;
else if(sec < t2.sec){
min -= 1; t1.sec = 60 + sec - t2.sec;}
//Minutes
//Same things go on here
//Hours
//Same things go on here
//Day
if(day > t2.day) t1.day = day - t2.day;
else if(day < t2.day){
mnth -= 1; t1.day = 30 + day - t2.day;}//Add 30 days, reduce t1 by a month
//Month
//Same things go on here
//Year
//Same stuffs........
//
return t1;
}
////EXTENDED PART OF THE PROGRAM//////////!!!!!!!!!!!!!!!!!!
/////////////////////////Timer class//////////////////////////////////
//Actually I don't know a better name to call this class
// suggestions will be appreciated
const int LIMIT = 1000;
class Timer : public Time {
private:
static int count;
char description[LIMIT];
Time *time1;
Time *time2;
Time *time_diff;
protected:
void calculate(); //Called for calculating time_diff
public:
Timer() //no-arg constructor
{ time1 = new Time; //Create new instances for all time
time2 = new Time;
time_diff = new Time;
count++; }
static int return_count();//shows number of calculations so far
void get_time(); //Gets input from User
void show_all(); //Shows all Time for object + desciption
~Timer()
{ delete time1; //Destructor, Deletes objects memory
delete time2;
delete time_diff;
}
};
////////////////Some Timer definition/////////////////////
//PROTECTED for calculating time_diff;
void Timer::calculate()
{
//Time_diff
//Remaining Time
*time_diff = *(time1) - *(time2);
time_diff->validate(); //Validate;
}
//For calculating time
void Timer::show_all()
{
//cout <<"\n---" << description <<"---\n";
cout <<">>>Time (1):\n";
time1->display();
cout <<"\n>>>Time (2):\n";
time2->display();
cout <<"\n>>>Time Difference<<<\n";
time_diff->display();
cout <<"\n-------------------------------"<< endl;
}
//Gets time input from user
void Timer::get_time()
{
do
{
cout <<"Enter Values for Time 1"<<endl;
time1->getTime(); //Get Time
time1->validate(); //Make sure it is valid
} while(time1->State() == 1);
do
{
cout <<"\n***Enter Values for Time 2>>>>\n";
time2->getTime(); //Get Time
time2->validate(); //Validate it
} while(time2->State() == 1);
/////////////////MY PROBLEM HERE!!!
///Why does the program program become weird after this stage... and giving me
///very werid result?
cout <<"\n Enter Comment:";
cin.getline(description, LIMIT, '\n');//**THIS LINE DOESN'T
//EVEN GET EXECUTED!
Timer::calculate();
//This function now behaves weird.
}
int main(){
const int rounds = 10;
int redoState = 0;
int n = 0;
Timer session[rounds];
//some intro and info here
do{
cin >> choice; cout << endl;
switch(choice)
{
case 'm':
session[n].get_time();
break;
case 's':
session[n].show_all();
break;
case 'i':
cout<<" Round:";
n = Input();
n -= 1;
break;
default:
cerr <<"\nERROR: Bad choice or Selection!\n";
break;
}
cout<<"\n\n\nContinue?\nEnter 1 for yes\nEnter 2 for no(Exit)\n";
cout <<"Option:";
redoState = Input();
} while(redoState != 2);
return 0;
}
all necessary headers are are in place... I'm not a complete beginner or maybe...in the eyes of geeks.
I plan to integrate a UI and upload it on sourceforge if I canI'm beginning to get frustrated, or do I rewrite (Timer) from scratch in a better way I'm thinking...?
*Also What kind of bug do you classify the program's problem into?