In my comsc class we are writing a program to determine when easter sunday is, my program works like a charm when i put each equation in a seperate function but when i try and take them out (as per teachers instructions) it craps the bed and gives me an infinite loop and garbage answers.
Prob3.dat input file is simple

1999
1989

thats it.

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

int main()
{
  //file holding all years we want to calc
  fstream datein ("Prob3.dat", ios::in);
  //year is the input year, date is the day calculated
  //na thru ne are the formula values;
  int year, date;
  int na, nb, nc, nd, ne;

  //no data or last data processed, loop will not run
  while(!datein.eof())
  {
    datein >> year;
    na = year % 19;
    nb = year % 4;
    nc = year % 7;
    nd = (19 * na + 24) % 30;
    ne = ((2 * nb) + (4 * nc) + (6 * nd) + 5) % 7;
    date = 22 + nd + ne;

  //output
    cout << "Easter is on ";
    //checking month and adjusting date value
    if(date > 31)
    {
      cout << "April ";
      date -= 31;
    }
    else
      cout << "March ";

    cout << date << " " << year << endl;
  }

  cin.get();
  return 0;
}

this version works perfectly with the same input file

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

//given equations
int Na(int n){return n % 19;};
int Nb(int n){return n % 4;};
int Nc(int n){return n % 7;};
int Nd(int n){return (19 * Na(n) + 24) % 30;};
int Ne(int n){return ((2 * Nb(n)) + (4 * Nc(n)) + (6 * Nd(n)) + 5) % 7;};
                     

int main()
{
  //file holding all years we want to calc
  fstream datein ("Prob3.dat", ios::in);
  //year is the input year, date is the day calculated
  int year, date;

  //no data or last data processed, loop will not run
  while(!datein.eof())
  {
    datein >> year;
    date = 22 + Nd(year) + Ne(year);

  //output
    cout << "Easter is on ";
    //checking month and adjusting date value
    if(date > 31)
    {
      cout << "April ";
      date -= 31;
    }
    else
      cout << "March ";

    cout << date << " " << year << endl;
  }

  cin.get();
  return 0;
}

Any help would be greatly appreciated, I'm sure it's something simple but you guys are always way more help than my ftard teach.

Recommended Answers

All 3 Replies

>> while(!datein.eof())
That is a problem because it will read the last line of the file twice. Here is how it should be coded while( datein >> year ) [edit]After changing the above while statement I don't get your problem[/edit]

Easter is on April 4 1999
Easter is on March 26 1989

quick note here, the above code can also be written as:

.
.
.
while(datein)
{
        datein >> year;
        if(datein.eof())       break;
        //Code here
}

To l4z3r:
It's not the same as Ancient Dragon's (RIGHT) solution!

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.