Can't get my program to work correctly

i am in a beginners c++ class and we are on our second program. I have been working on it for two weeks (remember I'm brand spanking new at all this) and my program compiles but does not have the desired end result.

Here are the directions:
Your program should consist of only one function, the main function. When
your program begins, the user is prompted to enter integer values representing
the month, day, and year, in that order. You do not need to validate the
date. The user’s entry is assumed to be a valid date. Your program should
then display [day-of-the-week-in-words], [month-in-words] [day], [year] and
indicate whether this date occurs in a leap year. Use a boolean variable
as a flag to indicate whether the year is a leap year or not.(The calendar
generator at http://www.timeanddate.com/calendar may be helpful to
you in verifying the correctness of your program.)

My program won't put out the month and day of the week in words, but other then that it works..here is my program let me know whats wrong:

#include <iostream>
using namespace std;

int main()
{
int month, day, year;
cout<<"Enter numeric values for month, day and year of a date>";
cin>>month>>day>>year;
bool yearLeap, monthWord, dayWord;
yearLeap = year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
if(month == 1)
monthWord = "January ";
else if(month == 2)
monthWord = "February ";
else if(month == 3)
monthWord = "March ";
else if(month == 4)
monthWord = "April ";
else if(month == 5)
monthWord = "May ";
else if(month == 6)
monthWord = "June ";
else if(month == 7)
monthWord = "July ";
else if(month == 8)

monthWord = "August ";
else if(month == 9)
monthWord = "September ";
else if(month == 10)
monthWord = "October ";
else if(month == 11)
monthWord = "November ";
else if(month == 12)
monthWord = "December ";
int u,v,w,x,y,century;
century = year / 100;
u = 2 * (3-(century%4));
v = century%100;
w = v/4;
if(month == 1)
{
if(month == yearLeap)
{
x = 6;
}
else
{
x = 0;
}
}
else if(month == 2)
{
if(month == yearLeap)
{
x = 2;
}
else
{
x = 3;
}
}
else if(month == 3)
{

x = 3;
}
else if(month == 4)
{
x = 6;
}
else if(month == 5)
{
x = 1;
}
else if(month == 6)
{
x = 4;
}
else if(month == 7)
{
x = 6;
}
else if(month == 8)
{
x = 2;
}
else if(month == 9)
{
x = 5;
}
else if(month == 10)
{
x = 0;
}
else if(month == 11)
{
x = 3;
}
else if(month == 12)
{

x = 5;
}
y = u+v+w+x+day;
dayWord = y%7;
if(dayWord == 0)
dayWord = "Sunday ";
if(dayWord == 1)
dayWord = "Monday ";
if(dayWord == 2)
dayWord = "Tuesday ";
if(dayWord == 3)
dayWord = "Wednesday ";
if(dayWord == 4)
dayWord = "Thrusday ";
if(dayWord == 5)
dayWord = "Friday ";
if(dayWord == 6)
dayWord = "Saturday ";
if(yearLeap == true)
{
cout<<monthWord<<dayWord<<", "<<year<<" occurred in a leap year.";
}
else
{
cout<<monthWord<<dayWord<<", "<<year<<" occurred in a non-leap year.";
}
return 0;
}
You're declaring the 'monthWord' and 'dayWord' as bool. bool can only be true or false. You need to use a std::string.

EDIT: Also, please use code tags when posting code.
[code]
// code... code... code...
[/code]
Last edited on
okay thanks i was trying to figure out how to do that little box. now i have these errors popping up

1
2
3
4
5
6
7
8
9
10

weekday.cpp: In function ‘int main()’:
weekday.cpp:118: error: no match foroperator==’ in ‘dayWord == 0’
weekday.cpp:120: error: no match foroperator==’ in ‘dayWord == 1’
weekday.cpp:122: error: no match foroperator==’ in ‘dayWord == 2’
weekday.cpp:124: error: no match foroperator==’ in ‘dayWord == 3’
weekday.cpp:126: error: no match foroperator==’ in ‘dayWord == 4’
weekday.cpp:128: error: no match foroperator==’ in ‘dayWord == 5’
weekday.cpp:130: error: no match foroperator==’ in ‘dayWord == 6’
I think you are intending to test your 'day' variable, not the 'dayWord' variable. Like this:

1
2
3
if (day == 0)
  dayWord = "Sunday";
...


Those errors are because you are comparing a std::string with an int. The compiler cannot find an appropriate overload for operator== to handle that comparison.

EDIT: Also, instead of using so many if statements like you did, I would suggest using if, else if, else or switch. Every one of those statements will be evaluated even if the day is 0. With if, else if, it will stop the comparisons as soon as it finds a match.
Last edited on
Post the exact code you are compiling and running now.
you are a genius! haha okay it is getting better...but still not putting out the day of the week in words...i changed to the if else statement and the dayWord to day.
this is what it looks like
1
2
3
cs125377@classes:~/prog2$ weekday
Enter numeric values for month, day and year of a date>10 31 1990
October 31, 1990 occurred in a non-leap year.cs125377@classes:~/prog2$


this is what its supposed to look like:
1
2
Enter numeric values for month, day and year of the a date> 10 31 1990
Wednesday, October 31, 1990 occurred in a non-leap year.
YAY I GOT IT TO WORK!! THANK YOU SOO MUCH!!!
Topic archived. No new replies allowed.