That may seem an isolated example, but it illustrates that having "using namespace std;" can cause unexpected consequences, without it being obvious that it has even happened. |
using namespace std;
utterly defeats the point in even having namespaces more or less?without looking at the code, in a year, you just like everyone else, will have no idea what month and dmonth represent. |
I also don't see any checks to make sure that the date entered is valid. So if the user enters 13 for the month, the program just accepts that, even though it's wrong. |
I also don't see any checks to make sure that the date entered is valid. So if the user enters 13 for the month, the program just accepts that, even though it's wrong. |
Last, when I declare int x; I like to set the value, else the computer uses memory that may already have a value. |
|
|
|
|
goto
and you can handle bad non-numeric input, too. So 'asfdfasj' as an input by the user won't blow up your program. However, cin is going to pull from the stream until it finds the first non-numeric, so strings like '11asdf' WILL get past this check and leave 'asdf' in the input buffer, so be careful! To mitigate this, you may want to put in a std::cin.sync();
after the closing brace for the while
loop.
Zacharee wrote: |
---|
Off-topic here, but a suggestion i would like to make for this site: Have a reply button on page one and/or make it so you can flip pages with the reply box open. |
I'm a little stumped about how to do this with with the day though. I'm sure I could work it out with some convoluted series of if-else statements, but I'm getting a nagging feeling theres an easier way to do it that I havn't been introduced to yet. |
int DaysInMonth(int month, bool isLeapYear);
|
|
So 'asfdfasj' as an input by the user won't blow up your program. |
|
|
|
|
goto cday;
in my invalidDayTest function is not sufficient to define the label cday in main (The error i get is "error: label 'cday' is used but not defined.) Is the fix just to define cday at some place in main? If so my book hasn't yet taught me how do define labels other than using goto.
In most web browsers, you can: -Middle click a link (e.g. page 1 or 2) to open it in a new tab -Right click on a tab and choose Duplicate, which also copies the forward and backward history But yeah, it would be lovely to have a button that automatically quotes a post, formatting and all. |
goto
. I suggested using a function because you could use it to separate your day validation logic from your interaction with the user. You can see how this function could be useful:
|
|
bool isLeapYear
. To my untrained eye it appears that you are initializing it as false and leaving no mechanism to change it to true. Is this bool based on the year input? If so would I have to shift somethings around so that the use inputs the year first so the program can test for a leap year before the day input? While I value you're contribution, it is getting a little out of my confort zone and I'm hestitant to use anything I don't understand in fear of building a habit of doing such. Although I probably will try to incorporate this eventually, because in theory your solution doesn't include anything I havn't learned yet, its just the way you are using some things that is intimidating me. So for now all I'm asking for is an explanation as to why my solution didn't work, and if possible, how to make it work. Remember I've only been at this for 3 days... baby steps XDTo my untrained eye it appears that you are initializing it as false and leaving no mechanism to change it to true |
|
|
|
|
static
means that memory is allocated for the data once, and other calls to the function use the same block of memory.class
, but I think too far ahead for you.
@booradley60: you try to return -1 in a function that returns an unsigned int. I would say that zero makes a better error value; |
|
|