We are supposed to write a program which asks for the month, date, and yr and then outputs the day of that date.

input:

Month:   7  // This is July
Day: 4
Year: 1776

output:

July 4, 1776  was on a Thursday

Formula:

day = (r + [2.6m - .2] - 2c + d + [c/4.0] +[d/4.0] ) % 7


[x] denotes the greatest integer less than or equal to x (look in cmath).
c  = first two digits of the year. (If the year is 1898, c = 18).
d  = the last two digits of the year. (If the  year is 1998, d = 98).
r  = the day of the month (If the date is May 23, 1998, r = 23).
m  = the month.   March is considered month 1, April is  2, May is 3...February is month 12.

this is my code [c++]:

#include<iostream>
#include<cmath>
using namespace std;


char Date(int c, int d, int r, int m);


int main()
{


int month, day, yr, yr1, yr2;


cout<<"month:"<<endl;
cin>>month;


cout<<"day:"<<endl;
cin>>day;


cout<<"year:"<<endl;
cin>>yr;


yr1=yr/100;
yr2=yr%100;


cout<<month<<" "<<day<<" "<<yr<<" "<<Date(yr1, yr2, day, month);
}


char Date(int c, int d, int r, int m)
{


char result;
result=ceil(r+(2.6*m-.2)-2*c+d+(c/4.0)+(d/4.0))%7;


switch(result)
{
case 1: cout<<" was on a sun";
break;
case 2: cout<<" was on a mon";
break;
case 3: cout<<" was on a tues";
break;
case 4: cout<<" was on a wed";
break;
case 5: cout<<" was on a thurs";
break;
case 6: cout<<" was on a fri";
break;
case 7: cout<<" was on a sat";
break;
default: cout<<"invalid";


return result;
}
}



//when i compile it, i get the error "'%' illegal, left operand, has type 'double""

Dani AI

Generated

— the compiler message means you tried to use the integer remainder operator % on a double. ceil() returns a double, and the formula calls for the greatest integer less than or equal to some subexpressions (use floor, not ceil). was right that you must convert to an integer before %, but there are a few other pitfalls to fix so the formula gives the right weekday.

Key fixes to apply:

  • Map the input month to the formula's month index (March=1 ... February=12): m = (month + 9) % 12 + 1. If the original month is January or February, decrement the year first (those months belong to the previous year in this scheme) and recompute c and d.
  • Apply floor() only to the bracketed terms in the formula: floor(2.6*m - 0.2), floor(c/4.0), floor(d/4.0).
  • Compute the sum as integers, then take the modulo. Ensure the result is forced into 0..6 even if the raw sum is negative.
  • Use int (or an enum) for the day index; char is unnecessary and can be confusing. Map 0=>Sunday, 1=>Monday, ... 4=>Thursday (which matches July 4, 1776).

Example (core logic only):

double a = floor(2.6*m - 0.2);
double b = floor(c / 4.0);
double e = floor(d / 4.0);
int raw = static_cast<int>(r + a - 2*c + d + b + e);
int dow = ((raw % 7) + 7) % 7;   // 0..6, 0 = Sunday

Troubleshooting checklist: make sure you adjust year for Jan/Feb before computing c/d; use floor() (not ceil()); cast to int before %; and switch on 0..6 for weekday names. For a simpler, robust alternative, consider using the C library struct tm + mktime() or C++ date utilities (chronology libraries) instead of hand-rolling the formula.

try this

char result;
result=(int)(ceil(r+(2.6*m-.2)-2*c+d+(c/4.0)+(d/4.0)))%7;

K.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.