I want to write a simple day of the week code

I just want any tips on how to finish the code to display the day of t

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 6392 Views - Last Post: 11 January 2010 - 11:04 PM Rate Topic: -----

#1 C++Rookie   User is offline

  • New D.I.C Head

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

I want to write a simple day of the week code

Posted 08 January 2010 - 08:00 PM

//DayofTheWeek - gives the abbreviation for the days of the week
//Created by <Bryant Mastrangelo> on <January 8th, 2010>

#include <iostream>
using namespace std;

class DayOfTheWeek
{
	public:
		    char monday; // first day of the week
			char tuesday; // second day of the week
			char wednesday; // third day of the week
			char thursday; // fourth day of the week
			char friday; // fifth day of the week
			char saturday; // sixth day of the week
			char sunday; // seventh day of the week
};

int main() {
	;DayOfTheWeek;.monday;
	;DayOfTheWeek;.tuesday;
	;DayOfTheWeek;.wednesday;
	;DayOfTheWeek;.thursday; 
	;DayOfTheWeek;.friday;
	;DayOfTheWeek;.saturday;
	;DayOfTheWeek;.sunday;  // create a day object
	
	// assign values to fields in monday to sunday
	;DayOfTheWeek;.monday = mon
	;DayOfTheWeek;.tuesday = tue
	;DayOfTheWeek;.wednesday = wed
	;DayOfTheWeek;.thursday = thur
	;DayOfTheWeek;.friday = fri
	;DayOfTheWeek;.saturday = sat
	;DayOfTheWeek;.sunday = sun

	cout << "DayOfTheWeek" << DayOfTheWeek.monday <<

	return 0;

};


I keep getting syntax error C2143 and C2600

Can you help me debug the code. I want it to print Day of the week monday is mon etc....

*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 08 January 2010 - 08:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I want to write a simple day of the week code

#2 athlon32   User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: I want to write a simple day of the week code

Posted 08 January 2010 - 08:43 PM

You are using classes wrong. That's why the code doesn't work

The class declaration itself is correct, but the way your trying to make an instance is wrong.
instead of:
 ;DayOfTheWeek;.monday;
        ;DayOfTheWeek;.tuesday;
        ;DayOfTheWeek;.wednesday;
        ;DayOfTheWeek;.thursday; 
        ;DayOfTheWeek;.friday;
        // etc....



you just need
DayOfTheWeek currentday; // make a DayOfTheWeek object


The other problem is here:
// assign values to fields in monday to sunday
        ;DayOfTheWeek;.monday = mon
        ;DayOfTheWeek;.tuesday = tue
        ;DayOfTheWeek;.wednesday = wed
        ;DayOfTheWeek;.thursday = thur
        ;DayOfTheWeek;.friday = fri
        ;DayOfTheWeek;.saturday = sat
        ;DayOfTheWeek;.sunday = sun


to acces data members of a class you use the format of instance_of_class.member. Also, if mon, tue, wed, etc are variables, they're obviously note in scope. If you're attempting to assign text for them to hold, the need to be in quotes like "mon". Also, char can't hold strings of text, only single characters. So in the class definition change them to char pointers like this:
char* monday; // first day of the week
                        char* tuesday; // second day of the week
                        char* wednesday; // third day of the week
                       // etc....



Hope i helped :D

This post has been edited by iphoneorange: 08 January 2010 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 12:47 AM

You might also like to look here:

http://www.dreaminco...mp;#entry569212
Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 01:35 AM

your syntax is very wrong, all though i dont understand the purpose of this program. your using c++ so trying taking a look a strings

#include <iostream>
#include <string>

using namespace std;

struct days
{
	string  monday,
			tuesday,
			wensday,
			thursday,
			friday;
	days(void)
	{
		monday = "MONDAY";
		tuesday = "TUESDAY";
		wensday = "WENSDAY";
		thursday = "THURSDAY";
		friday = "FRIDAY";
	}
		
};
	

int main()
{
	days *week = new days();

	cout<<week->monday<<endl
		<<week->tuesday<<endl
		<<week->wensday<<endl
		<<week->thursday<<endl
		<<week->friday<<endl;

	cin.ignore();
	cin.get();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 03:16 AM

If you would like it very 'simple' ... then you might like below:

#include <iostream>
#include <string>

using namespace std;

const string Days[] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

enum dayNum {sun, mon, tue, wed, thu, fri, sat};

int main()
{
    // show days of the week ...
    for( int i=0; i<7; ++i )
        cout << Days[i] << endl;

    cout << "\n or \n\n"
         << Days[sun] << endl
         << Days[mon] << endl
         << Days[tue] << endl
         << Days[wed] << endl
         << Days[thu] << endl
         << Days[fri] << endl
         << Days[sat] << endl;
         
    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.get();
}

Was This Post Helpful? 0
  • +
  • -

#6 C++Rookie   User is offline

  • New D.I.C Head

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

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 08:40 AM

View PostImaSexy, on 9 Jan, 2010 - 12:35 AM, said:

your syntax is very wrong, all though i dont understand the purpose of this program. your using c++ so trying taking a look a strings

#include <iostream>
#include <string>

using namespace std;

struct days
{
	string  monday,
			tuesday,
			wensday,
			thursday,
			friday;
	days(void)
	{
		monday = "MONDAY";
		tuesday = "TUESDAY";
		wensday = "WENSDAY";
		thursday = "THURSDAY";
		friday = "FRIDAY";
	}
		
};
	

int main()
{
	days *week = new days();

	cout<<week->monday<<endl
		<<week->tuesday<<endl
		<<week->wensday<<endl
		<<week->thursday<<endl
		<<week->friday<<endl;

	cin.ignore();
	cin.get();
	return 0;
}



[b]I copied and pasted your code to visual studio and on line 3 Error 1 error C2653: 'DayOfTheWeek' : is not a class or namespace name y:\d03141171\documents\visual studio 2005\projects\week1lab_bryantmastrangelo\week1lab_bryantmastrangelo\dayoftheweek.cpp 3

Also on line 4 Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int y:\d03141171\documents\visual studio 2005\projects\week1lab_bryantmastrangelo\week1lab_bryantmastrangelo\dayoftheweek.cpp 4

and line 5 Warning 3 warning C4508: 'DayOfTheWeek' : function should return a value; 'void' return type assumed y:\d03141171\documents\visual studio 2005\projects\week1lab_bryantmastrangelo\week1lab_bryantmastrangelo\dayoftheweek.cpp 5

ON LINE 7 Error 4 error C2653: 'DayOfTheWeek' : is not a class or namespace name y:\d03141171\documents\visual studio 2005\projects\week1lab_bryantmastrangelo\week1lab_bryantmastrangelo\dayoftheweek.cpp 7

Error 5 fatal error C1903: unable to recover from previous error(s); stopping compilation y:\d03141171\documents\visual studio 2005\projects\week1lab_bryantmastrangelo\week1lab_bryantmastrangelo\dayoftheweek.cpp 7
Was This Post Helpful? 0
  • +
  • -

#7 robhilly   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 26-May 08

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 08:45 AM

Quote

 

;DayOfTheWeek;.monday;
;DayOfTheWeek;.tuesday;
;DayOfTheWeek;.wednesday;
;DayOfTheWeek;.thursday; 
;DayOfTheWeek;.friday;
;DayOfTheWeek;.saturday;
;DayOfTheWeek;.sunday;




Though I am quite confused looking at these in a C++ program, I am curious to know where these statements originated. Perhaps the user who wrote this program has previously worked in a different programming or scripting language that uses the above syntax. There is known to be many programming languages out there. Knowing this would help assist this user in learning C++.
Was This Post Helpful? 0
  • +
  • -

#8 C++Rookie   User is offline

  • New D.I.C Head

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

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 08:51 AM

yeah the visual studio gave an error of wanting a ';' before DayOfTheWeek
Was This Post Helpful? 0
  • +
  • -

#9 athlon32   User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 09:05 AM

View PostC++Rookie, on 9 Jan, 2010 - 07:51 AM, said:

yeah the visual studio gave an error of wanting a ';' before DayOfTheWeek


Many times visual studio will give that error if you forget to end a line with a ';'.Most of the time, the error originates from the line above the line where the error is pointing to

This post has been edited by athlon32: 09 January 2010 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

#10 optix212   User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 09:09 AM

it doesn't mean that it wants a ';' before each and every DayOfTheWeek, it just means that you probably forgot to put a ';' after the statement before DayOfTheWeek.

quick example:

		DayOfTheWeek.monday = mon;
		DayOfTheWeek.tuesday = tue;
		DayOfTheWeek.wednesday = wed;
		DayOfTheWeek.thursday = thur;
		DayOfTheWeek.friday = fri;
		DayOfTheWeek.saturday = sat;
		DayOfTheWeek.sunday = sun;



please correct me if im wrong, as i am still a noob myself.

This post has been edited by optix212: 09 January 2010 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#11 C++Rookie   User is offline

  • New D.I.C Head

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

Re: I want to write a simple day of the week code

Post icon  Posted 09 January 2010 - 09:13 AM

I am open to suggestions on how to write a code in visual studio 2005 that displays the days of the week. I have written
 #include <iostream>
using namespace std;


const char* dayNames[] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

class Day
{
	public:
		Day() { dayNumber = 0; }
		const char* getDay() { return dayNames[dayNumber]; }
		const char* getNextDay() { return dayNames[(dayNumber+1) % 7]; }
		const char* getPrevDay() { return dayNames[(dayNumber+7-1) % 7]; }

		const char* getDayPlus( int n ) { return dayNames[(dayNumber+n) % 7]; }
		
		void setDay( int n ) { dayNumber = n; normalize(); }
		void printDay() { cout << dayNames[dayNumber]; }
	private:
		int dayNumber;
		void normalize() { dayNumber = dayNumber % 7; }
};


int main()
{
	Day d;
	cout << "The (default) day is " << d.getDay() <<endl
		 << "The next day is " << d.getNextDay() <<endl
		 << "The day plus 5 is " << d.getDayPlus(5) <<endl
		 << "The prev day is " << d.getPrevDay() <<endl;
		 
	d.setDay(6);
	cout << "After 'd.setDay(6)' ... the day now is " << d.getDay() <<endl
		 << "The next day is " << d.getNextDay() <<endl
		 << "The day plus 5 is " << d.getDayPlus(5) <<endl
		 << "The prev day is " << d.getPrevDay() <<endl;

	d.setDay(3);
	cout << "After 'd.setDay(3)' ... the day now is " << d.getDay() <<endl
		 << "The next day is " << d.getNextDay() <<endl
		 << "The day plus 12 is " << d.getDayPlus(12) <<endl
		 << "The prev day is " << d.getPrevDay() <<endl;

	cout << "The day still is ... ";
	d.printDay();

	cout << "\nPress 'Enter' to continue ... " << flush;
	cin.get();
}


I still get errors. I just want a simple abbreviated day like "mon" is monday etc....Can anyone help?

*mod edit: :code:
Was This Post Helpful? 0
  • +
  • -

#12 optix212   User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 09:17 AM

please dont post multiple threads about the same problem, you could have easily posted this in your other thread.

also, please remember to USE CODE TAGS.

here, read this:

http://www.dreaminco...wtopic13117.htm
Was This Post Helpful? 0
  • +
  • -

#13 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 09:38 AM

Threads merged, please do not create duplicate topics on the same problem.
Was This Post Helpful? 0
  • +
  • -

#14 C++Rookie   User is offline

  • New D.I.C Head

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

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 01:24 PM

I have tried to revise my code to display mon is monday, tues is tuesday etc... no luck. Can anyone help with my errors?

//DayofTheWeek - gives the abbreviation for the days of the week
//Created by <Bryant Mastrangelo> on <January 8th, 2010>
#include <iostream>
#include <string>
namespace std;
class dayOfTheWeek

{
public:

dayOfTheWeek(void); //Constructor
~dayOfTheWeek(void); //Destructor
void getDay(string&, string&);
void setDay(string, string);
void printday(string, string);
private:

string mon;
string tues;
};
dayOfTheWeek::dayOfTheWeek()

{
mon = "";
tues = "";
}
dayOfTheWeek::~dayOfTheWeek()
{
}

void dayOfTheWeek::setDay(string monday, string tuesday)
{
mon = monday;
tues = tuesday;
}
void dayOfTheWeek::getDay(string &monday, string &tuesday)
{
monday = mon;
tuesday = tues;
} void dayOfTheWeek::printday(string monday, string tuesday)
{
monday = mon;
tuesday = tues;
cout << "The value of the object " << monday << " is : "
<< mon << endl;
cout << "the value of the object " << tuesday << " is : "
<< tues << endl;

}
int main()
{
dayOfTheWeek DayOne;
dayOfTheWeek DayTwo;

DayOne.setDay(monday, tuesday);
DayTwo.setDay(monday, tuesday);

string localMon = "";
string localTues = "";

DayOne.getDay(localMon);
DayTwo.getDay(localTues);

DayOne.printday(localMon);
DayTwo.printday(localTues);

return 0;

};
Was This Post Helpful? 0
  • +
  • -

#15 C++Rookie   User is offline

  • New D.I.C Head

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

Re: I want to write a simple day of the week code

Posted 09 January 2010 - 01:58 PM

//DayofTheWeek - gives the abbreviation for the days of the week
//Created by <Bryant Mastrangelo> on <January 8th, 2010>
#include <iostream>
#include <string> 
namespace std 
class dayOfTheWeek //HERE IS MY ONE FATAL ERROR HOW DO I FIX Error	1	fatal error C1083: Cannot open include file: 'daysOfTheWeek.h': No such file or directory	y:\d03141171\documents\visual studio 2005\projects\week1lab_bryantmastrangelo\week1lab_bryantmastrangelo\dayoftheweek.h	11


//File: main.cpp 

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

//No real issue to use it here.
using namespace std; 

int main(int argc, char *argv[])
{    
	
	dayOfTheWeek dayOne;    
	dayOfTheWeek dayTwo;     
	//Setting the days, could have done this in the   
	//above 2 constructors aswell.	
	dayOne.setDay(dayOfTheWeek::Monday);	
	dayTwo.setDay(dayOfTheWeek::Tuesday);     
	//could just use toString, but just to show printDay	
	cout << "The days are: ";	
	dayOne.printDay(cout);	
	cout << " and ";	
	dayTwo.printDay(cout);	
	cout << ".\n";
	
	return 0;
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 10 January 2010 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2