got a question for my code.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: got a question for my code.

  1. #1
    Join Date
    Mar 2014
    Posts
    1

    got a question for my code.

    I have a code that calculates the number of days between dates. It considers leap years and different days among months. The problem I have is when i debug the code nothing comes out.

    Here is the code.
    #include<iostream>
    #include<fstream>
    using namespace std;

    bool wheep();
    int daysinmonth();

    bool wheep(int yr)
    {
    if (yr % 400 == 0)
    return true;
    else if (yr % 4 == 0 && yr % 100 != 0)
    return true;
    else
    return false;
    }
    int daysinmonth(int m, int y)
    {
    switch (m)
    {
    case 1:
    return 31;
    case 3:
    return 31;
    case 5:
    return 31;
    case 7:
    return 31;
    case 8:
    return 31;
    case 10:
    return 31;
    case 12:
    return 31;
    case 4:
    return 30;
    case 6:
    return 30;
    case 9:
    return 30;
    case 11:
    return 30;
    case 2:
    {
    if (wheep(y))
    return 29;
    else
    return 28;
    }
    default:
    cout << "error";
    }
    }
    int main()
    {
    int m1, d1, y1, m2, d2, y2, total;
    ifstream fin("dates.txt");
    fin >> m1 >> d1 >> y1 >> m2 >> d2 >> y2;
    if (m1 == m2 && y1 == y2)
    total = d2 - d1 + 1;
    else
    {
    total = daysinmonth(m1,y1) - d1 + 1;
    for (int i = m1 + 1; i < m2; ++i)
    total += daysinmonth(i, y1);
    total += d2;
    }

    while (!fin.eof())
    {
    cout << m1 <<" "<< d1 <<" "<< y1 <<" "<< m2 <<" "<< d2 <<" "<< y2 <<" "<<total<<endl;
    fin >> m1 >> d1 >> y1 >> m2 >> d2 >> y2;
    }
    fin.close();

    system("pause");
    return 0;
    }
    this is the file its reading
    1 7 9 1 10 9

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    Re: got a question for my code.

    Perhaps, the fin already reached the end-of-file?

    BTW, why do you write this code rather than use CRT date/time functions?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,590

    Re: got a question for my code.

    Quote Originally Posted by akim3 View Post
    when i debug the code nothing comes out
    You need to work on your debugging skills. Put a breakpoint before the section that prints stuff and find out why this code never is reached by your program.
    Nobody cares how it works as long as it works

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: got a question for my code.

    ... and please use code tags when posting code. The code as displayed is very unreadable. Go Advanced, select the code and click '#'.

    As Victor said in post #2, with the input file containing just 1 one line by the time the code comes to the while loop, fin is at the eof so the code within the while loop doesn't get executed so nothing gets printed.
    Last edited by 2kaud; March 19th, 2014 at 07:16 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center