0

I have an assignment where I'm given a file containing dates in numbered format and I have to have a program read the file and spit out days of the week for each date give. Each line of the file is a separate date. My go-to method was an eof loop. However, my file has 10 dates, and my output has 30. Here is my main for context.

int main()
{
    ifstream inFile;
    inFile.open("/Users/holdentatlow/Desktop/date 2");

    const char *Names[] = {"Sunday","Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday"};

    int day = 0;

    int date;

    inFile >> date;
    cout << "Day : " << Names[day] << endl;

    while (!inFile.eof()){

        inFile >> date;
        day = getWeekDay(date, date, date);
        cout<< "Day : "<< Names[day] <<endl;

    }

    inFile.close();

    return 0;
}

also, I get the impression that the dates it does report aren't entirely accurate. I don't have the resolve to check each one individually but the results seem erratic enough to not be simply repeated.

here's the file I'm taking dates from

0 10  1900
2 16 1900
1 2 1944
0 1 2004
1 29 2004
11 24 2002
6 2 2050
2 23 2004
0 6 1812
1 3 1800
  • 4
    _"My go-to method was an eof loop"_ Why? Who taught you to do that? – Lightness Races in Orbit Feb 26 '18 at 01:40
  • 1
    `day = getWeekDay(date, date, date);` What. – melpomene Feb 26 '18 at 01:41
  • hey just to address the two comments about theday = getWeekDay(date, date, date); part: it works. I'm sorry it it looks confusing but it works, thats not the problem – Holden Tatlow Feb 26 '18 at 01:42
  • 1
    Prefer constructor and destructor instead of `.open()` and `.close()`. –  Feb 26 '18 at 01:43
  • 3
    No, it _doesn't_ work, and it _is_ the problem. Why do you think it works and isn't the problem, when you get three times as many outputs as you expect and they're "not entirely accurate"? That seems to me to be a remarkable leap of faith. – Lightness Races in Orbit Feb 26 '18 at 01:43
  • @NickyC Good advice, but tell that to the C++ standard library. – melpomene Feb 26 '18 at 01:45
  • The first 10 outputs are accurate. The next 20 aren't, which are the problems in the first place. I'm asking for some clarification on what I'm doing wrong because obviously I know I'm doing something wrong, which is why I'm here in the first place. I'm using an eof loop because yes that's what I was taught, I apologize I didn't learn the same way as everybody else in the world but this is how things go in the community college I attend. I don't entirely appreciate the hostility many of these responses host, I would like guidance and I guess I'll look to other places in the future. – Holden Tatlow Feb 26 '18 at 01:48
  • 1
    It is impossible for the first 10 outputs to be accurate, except perhaps by pure chance due to your lack of error checking. If you feel you're starting to receive "hostility", perhaps interpret it more in the context of frustration that you're allegedly asking for some clarification on what you're doing wrong, but then rejecting everybody's answers to that question. If you start listening to us and drop the defensive attitude, we can get this fixed together. Also remember that this is free and it's Sunday night. – Lightness Races in Orbit Feb 26 '18 at 01:49
  • 1
    I don't see any hostility in anyone's comment. All I see are guidance. –  Feb 26 '18 at 01:51

1 Answers1

4

You seem to be trying to use date to mean three things at once.

int date;
inFile >> date;

Here you read one number. One. Not three; one.

while (!inFile.eof()){

Here you commit a cardinal sin; that is not how to loop over input.

inFile >> date;
day = getWeekDay(date, date, date);

Here you read another one number, and pass it to getWeekDay three times.

Your loop continues until there are no more numbers to read (sort of: see note above about that eof usage), which in your case will take three times as long as you expected (i.e. 30 not 10) because you read a third as many numbers as you thought (again, see above).

You'll want an >> operation for each number you want to read.

Here's an improved version of your program:

#include <fstream>
#include <istream>
#include <iostream>

// Assumed defined elsewhere
int getWeekDay(int date, int month, int year);

int main()
{
    std::ifstream inFile("/Users/holdentatlow/Desktop/date 2");

    static const char* Names[] = {
       "Sunday",
       "Monday",
       "Tuesday",
       "Wednesday",
       "Thursday",
       "Friday",
       "Saturday"
    };

    int date, month, year;
    while (inFile >> date >> month >> year) {
       const int day = getWeekDay(date, month, year);
       std::cout << "Day : " << Names[day] << std::endl;
    }
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055