Data Structure C++

Designing and implementing a class dayType that implements the day of

Page 1 of 1

8 Replies - 7672 Views - Last Post: 21 January 2010 - 01:37 AM Rate Topic: -----

#1 JamesG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-January 10

Data Structure C++

Posted 14 January 2010 - 11:31 PM

Good day. Everyone. I've given an assignment on C++ Data structure. I'm using Linux (ubuntu) C++ Compiler on it. The question is like this:

1a). Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type dayType:

a. Set the day
b. Print the day
c. Return the day
d. Return the next day
e. Return the previous day
f. Calculate and return the day by adding certain days to the current day. For example if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
g. Add the appropriate constructors.

1b). Then, write the definitions of the functions to implement the operations for the class dayType as defined in Programming above. Also, write a program to test various operations on this class.

I've finish all the required part but sadly I've encountered errors on the (Sunday and Saturday part) during the run. * I was so close to the final result, can anybody guide/help me on my errors. Thank you and God Bless you.

The errors said *segmentation fault. The following are my Header file, Implementation file and Main file.

//Header file

#include<iostream>
#include<string>
using namespace std;

class dayType
{
public:
dayType();

string day[8];
int dayNumber;
int tempDay;

void setDay(int day);
void printDay();
void returnDay(int &day);
void returnNextDay();
void returnPreviousDay();
void calculateDay(int changeDay);

};


//Implementation file

#include <iostream>
#include <string>
#include "dayType.h"

using namespace std;

void dayType::printDay()
{
cout << "Today is: " << day[dayNumber] << "day" << endl;};

void dayType::setDay(int day)
{
dayNumber=day;};

void dayType::returnDay(int &day)
{
day=dayNumber;};

void dayType::returnNextDay()
{
dayNumber++;};

void dayType::returnPreviousDay()
{
dayNumber--;};

void dayType::calculateDay(int changeDay)
{
tempDay=(dayNumber+changeDay);
dayNumber=(tempDay%7);};


//Main file

#include <iostream>
#include "dayType.h"

using namespace std;

int main()
{
dayType today;

int day;
int changeDay;

cout << "------------------------------------------------------------------" << endl;
cout << "Default day is Sunday " << endl;
cout << "Type in a number corresponding to set the day" << endl
<< "1: Monday" << endl
<< "2: Tuesday" << endl
<< "3: Wednesday" << endl
<< "4: Thursday" << endl
<< "5: Friday" << endl
<< "6: Saturday" << endl
<< "7: Sunday" << endl;

while (day<0 || day>7)
cin >> day;

today.setDay(day);
today.printDay();

today.returnDay(day);
today.printDay();

cout << "-----------------------------------------------------------------" << endl;
cout << "If it were tomorrow, then the following statement would be true: " << endl;
today.returnNextDay();
today.printDay();
today.dayNumber--;

cout << "-----------------------------------------------------------------" << endl;
cout << "If it were yesterday, then the following statement would be true:" << endl;
today.returnPreviousDay();
today.printDay();
today.dayNumber++;

cout << "-----------------------------------------------------------------" << endl;
cout << "Add a number of days to today and see what day it will be: " << endl;
cin >> changeDay;

today.calculateDay(changeDay);
today.printDay();

return 0;
};

dayType::dayType()
{
dayNumber=1;
day[1]="Mon";
day[2]="Tues";
day[3]="Wednes";
day[4]="Thurs";
day[5]="Fri";
day[6]="Satur";
day[7]="Sun";
};




Is This A Good Question/Topic? 0
  • +

Replies To: Data Structure C++

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Data Structure C++

Posted 15 January 2010 - 12:33 AM

I get this when I run the program:

Quote

Run-Time Check Failure #3 - The variable 'day' is being used without being initialized.


...
int day;
...
	while (day<0 || day>7)
		cin >> day;
...



WinXP vs2008

This post has been edited by n8wxs: 15 January 2010 - 12:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Data Structure C++

Posted 15 January 2010 - 09:17 AM

That (not initializing that variable) is definitely a problem. Also, your setDay(), returnNextDay() and returnPreviousDay() methods can set the dayNumber to invalid values (you may not have noticed that because your main() function didn't try all the possibilities).

It's very odd that you have 3 methods named "return*Day", none of them return anything, and two of them are actually "setters", not "getters" (they set the value of a member variable).

It's not a good idea to use the same name ("day") for a member variable (the string array) and also for the arguments to various member functions.
Was This Post Helpful? 0
  • +
  • -

#4 ben1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-January 10

Re: Data Structure C++

Posted 15 January 2010 - 12:05 PM

View Postr.stiltskin, on 15 Jan, 2010 - 08:17 AM, said:

That (not initializing that variable) is definitely a problem. Also, your setDay(), returnNextDay() and returnPreviousDay() methods can set the dayNumber to invalid values (you may not have noticed that because your main() function didn't try all the possibilities).

It's very odd that you have 3 methods named "return*Day", none of them return anything, and two of them are actually "setters", not "getters" (they set the value of a member variable).

It's not a good idea to use the same name ("day") for a member variable (the string array) and also for the arguments to various member functions.


you should have a constructor that sets all the member variables to a default value

This is always a good idea.

Chip
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Data Structure C++

Posted 15 January 2010 - 01:20 PM

View Postben1991, on 15 Jan, 2010 - 02:05 PM, said:

you should have a constructor that sets all the member variables to a default value

This is always a good idea.

Chip

Actually he does. You probably didn't notice it because it is at the bottom of the code, after the main function.
Was This Post Helpful? 0
  • +
  • -

#6 ayin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 10

Re: Data Structure C++

Posted 20 January 2010 - 10:11 AM

PLEASE SOMEBODY SOLVE THIS PROBLEM..I GOT THE SAME QUESTION FOR MY LAB TEST..PLEASE..THANK U SO MUCH
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Data Structure C++

Posted 20 January 2010 - 10:16 AM

Your day variable is not initalized so it is not even getting past your while loop. This is the type of situation where you want a do-while{} loop
do{
	cin >> day;
}while(day<0||day>7);


Was This Post Helpful? 0
  • +
  • -

#8 sonicpp  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 58
  • Joined: 08-January 10

Re: Data Structure C++

Posted 20 January 2010 - 01:32 PM

I don't like the way you handle the string array... you set it up to be 8 strings and then set the array from day[1] to day[7].
//Header file

#include<iostream>
#include<string>
using namespace std;

class dayType
{
public:
dayType();

string day[8];
 // rest of code...

dayType::dayType()
{
dayNumber=1;
day[1]="Mon";
day[2]="Tues";
day[3]="Wednes";
day[4]="Thurs";
day[5]="Fri";
day[6]="Satur";
day[7]="Sun";
};


In C++ arrays are index from 0. You should use them that way.
Here is the way I feel it should be done...
//Header file

#include<iostream>
#include<string>
using namespace std;

class dayType
{
public:
dayType();

string day[0];
 // rest of code...

dayType::dayType()
{
dayNumber=1;
day[0]="Mon";
day[1]="Tues";
day[2]="Wednes";
day[3]="Thurs";
day[4]="Fri";
day[5]="Satur";
day[6]="Sun";
};



Of course you could just declare it as such
string day[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};


Was This Post Helpful? 0
  • +
  • -

#9 JamesG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-January 10

Re: Data Structure C++

Posted 21 January 2010 - 01:37 AM

View Postsonicpp, on 20 Jan, 2010 - 12:32 PM, said:

I don't like the way you handle the string array... you set it up to be 8 strings and then set the array from day[1] to day[7].
//Header file

#include<iostream>
#include<string>
using namespace std;

class dayType
{
public:
dayType();

string day[8];
 // rest of code...

dayType::dayType()
{
dayNumber=1;
day[1]="Mon";
day[2]="Tues";
day[3]="Wednes";
day[4]="Thurs";
day[5]="Fri";
day[6]="Satur";
day[7]="Sun";
};


In C++ arrays are index from 0. You should use them that way.
Here is the way I feel it should be done...
//Header file

#include<iostream>
#include<string>
using namespace std;

class dayType
{
public:
dayType();

string day[0];
 // rest of code...

dayType::dayType()
{
dayNumber=1;
day[0]="Mon";
day[1]="Tues";
day[2]="Wednes";
day[3]="Thurs";
day[4]="Fri";
day[5]="Satur";
day[6]="Sun";
};



Of course you could just declare it as such
string day[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};



Quote

Thanks everyone for your kind response to my problem but I've already figure out my problem by myself and done it perfectly. Even my lecturer said it was tricky method but it was a success.

I won't post my complete coding here in order to prevent plagiarism esp for those "leeches" who just want to cheat in their test or ask people do their homework.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1