greeting everyone, a little help? thanks.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

greeting everyone, a little help? thanks.

 
0
  #1
Oct 18th, 2004
Good evening all, I am now working on a project for my intro c++ course and I'm having alot of difficulty understanding how to make a class program. I've been looking over the text but I am still confused. I would really appreciate any suggestions, ideas , or ways to get the program working. Here is the question:

Design a class called Date. The class should store a date in three integers: month, day, and year. There should be member functions to print the date in the following forms:

3/29/04

March 29, 2004

29 March 2004

Use a private member function to set the month name based on the month number. Demonstrate the class by writing a complete program implementing it.

Input Validation: Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1.

#include <iostream>
using namespace std;

// Class declaration
class date
{

private:
int monthName;
public:
int month;
int days;
int year;
};


That is all I could manage to do.. for "Use a private member function to set the month name based on the month number"

am I going to use if>else statements for every single month from 01=Jan to 12=dec , etc?

I am really confused about how to proceed in this program so any help is really appreciated. Thank you.

wangsta.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: greeting everyone, a little help? thanks.

 
0
  #2
Oct 18th, 2004
Greetings,

Looking through your statement, I did notice the "Use a private member function to set the month name based on the month number" quote.

Let's disect this statment, as it holds the key of the entire answer. Let's take it from the end, back.

Firstly, "based on the month number" seems to state that the month number has to be given by the user. Secondly, "Use a private member function" tells us we need to create a function in the private section of our class. Lastly, "set the month name" must tell us that this must be a variable; either public or private. I would suggest private as noting the fact they want a private function.

Let's take a closer look:
class Date {
public:
	void SetDate(int, int, int);
	void ShowDate(void);
private:
	char *MonthName(int);
	int year, month, day;
};
This may not make sense, and I added some stuff in there they don't talk about. There is one function called SetDate() in the public area. Why is this here? So we can change year, month, and day in our private section without access warnings/errors.

ShowDate() on the other hand will print the final string. Our MonthName() function will be designed to take the month number in and return the name corresponding to the month given. Let me help you get started and show you how SetDate() will work:
void Date::SetDate(int y, int m, int d) {
	year = y;
	month = m;
	day = d;
}
Remember, once you are inside the class function, you have direct access to private members. Since year, month, and day are initialized in the private section of the class, our function SetDate() takes our variables from anywhere and modifies the private members behind the scenes.

The function MonthName is not hard at all, just use a simple if/else statement, and all will fall under place:
char *Date::MonthName(int m) {
	if (m == 1)
		return "January";

	return NULL;
}
Simple, if it is 1 return the string January, though if not, return nothing.

ShowDate() will use year, month and day, convert month to string and display to the screen what the current year/month/day string it is.

I hope this helps. If you have any further questions, please feel free to ask.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

Re: greeting everyone, a little help? thanks.

 
0
  #3
Oct 19th, 2004
Hi Stack overflow:

Thanks for all the heads up. I followed your instructions but I am getting an error when executing :

Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/project3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

project3.exe - 2 error(s), 0 warning(s)

Here is my Code:

#include <iostream>
using namespace std;

// Class declaration
class Date {
public:
void SetDate(int, int, int);
void ShowDate(void);
private:
char *MonthName(int);
int year, month, day;
};
void Date:etDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
char *Date::MonthName(int m) {
if (m == 1)
return "January";
if (m == 2)
return "February";
if (m == 3)
return "March";
if (m == 4)
return "April";
if (m == 5)
return "May";
if (m == 6)
return "June";
if (m == 7)
return "July";
if (m == 8)
return "August";
if (m == 9)
return "September";
if (m == 10)
return "October";
if (m == 11)
return "November";
if (m == 12)
return "December";


return NULL;
}

Am I supposed to use if/else statements? Also, is there a good textbook you can recommend for first time c++ users that's easy to follow? My current textbook is not very good and unfortunately the class isn't very good either. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: greeting everyone, a little help? thanks.

 
0
  #4
Oct 19th, 2004
Greetings wangstarr,

The following error simply means you have no main() in your source code:

error LNK2001: unresolved external symbol _main

It is simple to fix, just add somewhere in your source:
int main() {
	Date date;	// Create a declaration

	date.SetDate(2004, 10, 19); // 10/19/'04

	return 0;
}
Speaking of MonthName(), using an if/else statement would probably be recommended.

if-else statement
The if-else statement is used to express decisions. Formally, the syntax is:
if (expression)
	statement1
else
	statment2
The else part is optional.

else-if statement
The Else-If statement has a syntax of:
if (expression)
	statement
else if (expression)
	statement
else
	statement
The sequence of if statments is the most general way of writing a multi-way decision. The expressions are evaluated in order; if any expression is true, the statment associated with it is executed, and this terminates the whole chain. As always, the code for each statment is either a single statement, or a group in braces.

The last else part handles the "none of the above" or default case where none of the other conditions is satisfied.

C++ Books
The Beginner's Guide to C++ by Oleg Yaroshenko
Learn to Program with C++ by John Smiley

There are some other books, but not exactly geared toward beginning the language. Those books are good for the novice/beginners.

If you have further questions, please feel free to ask.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

Re: greeting everyone, a little help? thanks.

 
0
  #5
Oct 19th, 2004
Thanks for the quick response. I will be sure to look into those books. I've changed my code to the following but when I am executing the program it just says "press any key to continue" and closes the program. Did I insert the main() in the wrong part of the program?


#include <iostream>
using namespace std;

// Class declaration
class Date {
public:
void SetDate(int, int, int);
void ShowDate(void);
private:
char *MonthName(int);
int year, month, day;
};
void Date:etDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
int main() {
Date date; // Create a declaration

date.SetDate(2004, 10, 19); // 10/19/'04

return 0;
}

char *Date::MonthName(int m) {
if (m == 1)
return "January";
else if (m == 2)
return "February";
else if (m == 3)
return "March";
else if (m == 4)
return "April";
else if (m == 5)
return "May";
else if (m == 6)
return "June";
else if (m == 7)
return "July";
else if (m == 8)
return "August";
else if(m == 9)
return "September";
else if (m == 10)
return "October";
else if(m == 11)
return "November";
else if(m == 12)
return "December";


return NULL;

}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: greeting everyone, a little help? thanks.

 
0
  #6
Oct 19th, 2004
Greetings,

It is compilers fault for making the the screen say "press any key to continue" and then close. Your main() declaration is fine, though before you "return 0;" you may want to put in "cin.get()":
#include <iostream>
using namespace std;

int main() {
	cin.get();

	return 0;
}

Some don't exactly teach what get() does or how it works, though I think it is beneficial to learn every code given:

get() extracts a character from the stream and returns its value. Somewhat like getch(), a non-standard function, produces as it waits for a keypress before closing the screen.

All we are doing is waiting for a key press, literally pausing the screen until user-closed.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 238
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: greeting everyone, a little help? thanks.

 
0
  #7
Oct 19th, 2004
Maybe you were planning to implement your ShowDate function and call it before exiting, but you didn't.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

Re: greeting everyone, a little help? thanks.

 
0
  #8
Oct 19th, 2004
Hi, sorry to trouble you guys again but I am still getting a blank program when executing. I've added the cin.get() code as suggested but it's still not functioning, thanks.

#include <iostream>
using namespace std;

// Class declaration
class Date {
public:
void SetDate(int, int, int);
void ShowDate(void);
private:
char *MonthName(int);
int year, month, day;
};
void Date:etDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
int main() {
Date date; // Create a declaration
date.SetDate(2004, 10, 19); // 10/19/'04
cin.get();
return 0;
}

char *Date::MonthName(int m) {
if (m == 1)
return "January";
else if (m == 2)
return "February";
else if (m == 3)
return "March";
else if (m == 4)
return "April";
else if (m == 5)
return "May";
else if (m == 6)
return "June";
else if (m == 7)
return "July";
else if (m == 8)
return "August";
else if(m == 9)
return "September";
else if (m == 10)
return "October";
else if(m == 11)
return "November";
else if(m == 12)
return "December";


return NULL;

}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 238
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: greeting everyone, a little help? thanks.

 
0
  #9
Oct 19th, 2004
*Ahem*
Originally Posted by Dave Sinkula
Maybe you were planning to implement your ShowDate function and call it before exiting, but you didn't.
Please point to the line of code you think should generate some output.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: greeting everyone, a little help? thanks.

 
0
  #10
Oct 19th, 2004
Alright,

Down to the last function in your class to make is ShowDate()

This function will take the year, month, and date from your class and build text to display. Before we move onto creating the code, all we are going to do is print the day, the month (by name), and the year.

Here is another code for your class:
void Date::ShowDate(void) {
	cout << MonthName(month) << " " << day << ", " << year;
}
The way this works is it prints text using "cout", the output stream and prints the month name of our current month. That's what our MonthName code is for. The it prints the day, and then the year.

Now after
date.SetDate(2004, 10, 19); // 10/19/'04
Just add:
  1. date.ShowDate();
This should display the current time entered by setDate().


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC