help

C

C++ newbie

I am trying to write a program that returns the dayNumber for a given
date. I have wrote a program that uses nested if statements. The
problem is it only executes the first if statement and nothing else. I
will post a little of the code to see if anyone can help. All input
appreciated!!!
int month;
int day;
int year;
int dayNumber = 0;

cout<< "Enter a date: ";
cin >> month;
cin >> day;
cin >> year;

if(month > 1)
dayNumber = 31;
else if(month > 2)
dayNumber = 31 + 28;
else if(month > 3)
dayNumber = 31 + 28 + 31;
else if(month > 4)
dayNumber = 31 + 28 + 31;
else if(month > 5)
dayNumber = 31 + 28 + 31 + 30;
else if(month > 6)
dayNumber = 31 + 28 + 31 + 30 + 31;
else if(month > 7)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30;
else if(month > 8)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31;
else if(month > 9)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
else if(month > 10)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
else if(month > 11)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
else if(month > 12)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
+ 30;
 
R

rajkumar

12 month is greater then 1 so your first condition is always true.

try

if ( month > 12 )
else if ( month > 11 )
etc.


Raj
 
J

Jeff Schwab

C++ newbie said:
I am trying to write a program that returns the dayNumber for a given
date. I have wrote a program that uses nested if statements. The
problem is it only executes the first if statement and nothing else. I
will post a little of the code to see if anyone can help. All input
appreciated!!!
int month;
int day;
int year;
int dayNumber = 0;

cout<< "Enter a date: ";
cin >> month;
cin >> day;
cin >> year;

if(month > 1)
dayNumber = 31;
else if(month > 2)
dayNumber = 31 + 28;
else if(month > 3)
dayNumber = 31 + 28 + 31;
else if(month > 4)
dayNumber = 31 + 28 + 31;

Whoops, you missed one.
else if(month > 5)
dayNumber = 31 + 28 + 31 + 30;
else if(month > 6)
dayNumber = 31 + 28 + 31 + 30 + 31;
else if(month > 7)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30;
else if(month > 8)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31;
else if(month > 9)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
else if(month > 10)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
else if(month > 11)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
else if(month > 12)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
+ 30;


int day_number ( int month, int day ) {
// assert( not a leap year );
// assert( month and day in range );
static int days_before[ ] = {
0,
31,
31 + 28,
31 + 28 + 31,
31 + 28 + 31 + 30,
31 + 28 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
};

return days_before[ month - 1 ] + day;
}

template< typename Result,
typename Ostream,
typename Istream,
typename String >
Result prompt ( Ostream& out, Istream& in, String const& message) {

Result result;

out << message;

if( ! ( in >> result ) ) {
throw "prompt: input error\n";
}

return result;
}

#include <iostream>

int main ( )
try {
int month = prompt< int >( std::cout, std::cin,
"Enter month as integer (1-12): " );

int day = prompt< int >( std::cout, std::cin,
"Enter day as integer (1-31): " );

std::cout << "day number is " << day_number( month, day )
<< "\n";
return 0;
}
catch( char const* s ) {
std::cerr << s;
return -1;
}
 
O

Old Wolf

C++ newbie said:
I am trying to write a program that returns the dayNumber for a given
date. I have wrote a program that uses nested if statements. The
problem is it only executes the first if statement and nothing else.
if(month > 1)
dayNumber = 31;
else if(month > 2)
dayNumber = 31 + 28;
else if(month > 3)
dayNumber = 31 + 28 + 31;
else if(month > 4)
dayNumber = 31 + 28 + 31;
else if(month > 5)
dayNumber = 31 + 28 + 31 + 30;
else if(month > 6)
dayNumber = 31 + 28 + 31 + 30 + 31;
else if(month > 7)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30;
else if(month > 8)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31;
else if(month > 9)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
else if(month > 10)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
else if(month > 11)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
else if(month > 12)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
+ 30;

What do you think "else" means ??

Also, when is month ever greater than 12?

Try this:

dayNumber = 0;
switch(month)
{
case 12: dayNumber += 30;
case 11: dayNumber += 31;
case 10: dayNumber += 30;
case 9: dayNumber += 31;
case 8: dayNumber += 31;
case 7: dayNumber += 30;
case 6: dayNumber += 31;
case 5: dayNumber += 30;
case 4: dayNumber += 31;
case 3: dayNumber += 28; /* add leap-year clause here */
case 2: dayNumber += 31;
}
 
G

gurumare

Basically you can't use the "else" statements here, because if ther
first one is true, which it will be, the rest won't excecute. to alude
to a previous poster, you have to start at the highest, which won't
always be true, and work your way down. plus there has to be a way to
reduce redundant code. try calculating them yourself and then inputing
the value, ie if month is 2, then day number is gonna = 59, so just
make it 59 and skip all the extra work. You also have to consider what
happens when you pull in 3 integers (3 cin statements) and the user
does not use the month/day/year format. just something to consider.
 
T

Thomas Matthews

C++ newbie said:
I am trying to write a program that returns the dayNumber for a given
date. I have wrote a program that uses nested if statements. The
problem is it only executes the first if statement and nothing else. I
will post a little of the code to see if anyone can help. All input
appreciated!!!
int month;
int day;
int year;
int dayNumber = 0;

cout<< "Enter a date: ";
cin >> month;
cin >> day;
cin >> year;

if(month > 1)
dayNumber = 31;
else if(month > 2)
dayNumber = 31 + 28;
else if(month > 3)
dayNumber = 31 + 28 + 31;
else if(month > 4)
dayNumber = 31 + 28 + 31;
else if(month > 5)
dayNumber = 31 + 28 + 31 + 30;
else if(month > 6)
dayNumber = 31 + 28 + 31 + 30 + 31;
else if(month > 7)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30;
else if(month > 8)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31;
else if(month > 9)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
else if(month > 10)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
else if(month > 11)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
else if(month > 12)
dayNumber = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
+ 30;

const unsigned int days_per_month[] =
{ /* 0 -- no month, no values */ 0,
/* Jan */ 31, /* Feb */ 28, /* Mar */ 31,
/* Apr */ 30, /* May */ 31, /* Jun */ 30,
/* Jul */ 31, /* Aug */ 31, /* Sep */ 30,
/* Oct */ 31, /* Nov */ 30, /* Dec */ 31
};

dayNumber = 0;
for (unsigned int i = 1; i <= month; ++i)
{
dayNumber = dayNumber + days_per_month;
}

if (/* year is leap year */)
{
++dayNumber;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
V

Victor Bazarov

Thomas said:
[...]
const unsigned int days_per_month[] =
{ /* 0 -- no month, no values */ 0,
/* Jan */ 31, /* Feb */ 28, /* Mar */ 31,
/* Apr */ 30, /* May */ 31, /* Jun */ 30,
/* Jul */ 31, /* Aug */ 31, /* Sep */ 30,
/* Oct */ 31, /* Nov */ 30, /* Dec */ 31
};

dayNumber = 0;
for (unsigned int i = 1; i <= month; ++i)
{
dayNumber = dayNumber + days_per_month;
}

if (/* year is leap year */)
{
++dayNumber;
}


What I don't understand is why do you need a loop when you can simply put
the right numbers into the array...

const unsigned int daynumber_offset[] = { 0, 31, 59, 90,
120, 151, 181, 212, 243, 273, 304, 334 };
dayNumber = date + daynumber_offset[month]
+ (is_leap_year(year) && month > february);

....

V
 
N

Noah Roberts

C++ newbie said:
I am trying to write a program that returns the dayNumber for a given
date.

I don't know why nobody has mentioned this yet, but you have a lot of
functions in the ctime header that might help you out a lot. Look
particularly at the tm structure and functions associated with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top