the system tell me "the days is not defined":will there anyone help me?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wild wolf
    New Member
    • Aug 2010
    • 5

    #1

    the system tell me "the days is not defined":will there anyone help me?

    Code:
    #include <iostream>
    
    int main()
    {
     int year,month,date,days,feb;
     cout<<"please input a date(eg. 2010 8 10)";
     cin>>year>>month>>date;
     if((year%4==0&&year%100!=0)||year%400==0) 
      feb=29;
     else 
      feb=28;
     switch (month)
     {
      case 12: days=30;
      case 11: days=days+31;
      case 10: days=days+30;
      case 9: days=days+31;
      case 8: days=days+31;
      case 7: days=days+30;
      case 6: days=days+31;
      case 5: days=days+30;
      case 4: days=days+31;
      case 3: days=days+feb;
      case 2: days=days+31;
      case 1: days=days+0;
     }
     days=days+date;
     cout<<"The day is the"<<days<<"in a year";
     return 0;
    }
    Last edited by Niheel; Aug 16 '10, 07:25 PM.
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Try initializing days; it looks like you're getting a warning about days not having an assigned value at "case 12"

    Code:
    int year,month,date,days=0,feb;
    Good Luck!

    Comment

    • wild wolf
      New Member
      • Aug 2010
      • 5

      #3
      I have tryed to give the days a first value. but it seemed that it is not this matter....

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This compiles OK with Visual Studio.NET 2008.

        What error, exactly, are you getting?

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Post the exact error message, please?

          Also, your switch doesn't have a default branch - unless you initialize days, the compiler can't count on days being assigned at the point where you add date to it.

          Comment

          • wild wolf
            New Member
            • Aug 2010
            • 5

            #6
            Click image for larger version

Name:	error.jpg
Views:	1
Size:	14.0 KB
ID:	5410558 this is the error image. And i download a vc++ 6.0 for it , find it is ok despite of the wrong result …… In addition , i add the "using namespace std;"

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              The error message is telling you that there is a code path where days is used without first being initialized to a known value.

              So why doesn't the initialization of days as part of its definition resolve the warning that you're getting?

              I suppose the other thing you could do is move days to a definition of its own, like this:
              Code:
              int days = 0;
              Try it, and good luck. I just don't see what's wrong, otherwise.

              Comment

              • wild wolf
                New Member
                • Aug 2010
                • 5

                #8
                it is successful , i use vc++ to compile it . you are right , when i give 0 to the days , it return a right result , thank you for your help^^

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  @wild_wolf,

                  You are quite welcome.

                  I learned something about the compiler's eccentricities, as well.

                  Cheers!

                  Comment

                  Working...