CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  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
    Hanover Germany
    Posts
    20,396

    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,641

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured