Nested Else-Ifs Do Not Go Beyond The First Else-if

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #16
    Then know that this is exactly what is happening when we use your style. You can't say a code is working because most conditions work, and one or two don't.
    I never said the code is working. The code is not working. That's why you asked your question. I only said the technique can work, if done correctly.

    Why does not my code account for this? Which other scenarios can you think of in the last "else" that remains? So you completely ignored this "else". And that has been your problem throughout.
    The scenario that is not accounted for is the one I already mentioned. Your code doesn't account for when month is equal to b_month and day is greater than b_day. With the nested if, your else is not reached because it first goes to the month == b_month branch of code. I did account for your else clause. You just misunderstood when that else clause is reached when using nested ifs.

    It is not about RECOMMENDATION. The question was, to SEE if things can work this way FOR THIS PARTICULAR CODE, since this style of nested else-if CANNOT at all work past the first else-if, but since I might be forgetting something. Like I said in my opening post: "Though the point that should be taken here is that it won't work this way, and which is all that should matter." The word "recommendation " can apply only where the option is ALWAYS available. Here, this option can be had only when there are far too less conditions.
    You're wrong, the option is available, you just have to do it correctly.

    I was hoping to guide you to the answer but at this point it may be better to just give you the answer so you can see what I'm talking about. This is the solution using nested ifs.

    Code:
    string name;
    int day, month, year, b_day, b_month, b_year;
    
    cout <<"Enter your first name: "; 
    cin >>name;
    
    cout <<"H! " <<name <<", what is the date today? Enter day (1 to 31): "; 
    cin >>day;
    
    cout <<"Now enter the month (1 to 12): "; 
    cin >>month;
    
    cout <<"And the year? (4 digits): "; 
    cin >>year;
     
    cout <<"\nNow input your birth date the same way. Day: "; 
    cin >>b_day;
    
    cout <<"Month: "; 
    cin >>b_month;
    
    cout <<"Year: "; 
    cin >>b_year;
     
    if (month < b_month) {
       cout <<"\nYour age is: " <<year - b_year - 1 <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
    } else if (month == b_month) {
       if (day < b_day) {
          cout <<"\nYour age is: " <<year - b_year - 1 <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
       } else {
          if (day == b_day) cout <<"\nHey... you became " <<year - b_year <<" years old today on " <<day <<"-" <<month <<"-" <<year <<"... Happy Birthday!!";
       }
    } else {
       cout <<"\nYour age is " <<year - b_year <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
    }

    Comment

    • HolyDoom Witch
      New Member
      • Oct 2012
      • 34

      #17
      Ahem... Passengers, Attention Please:
      Since DonBock has written everything in accordance with Rabbit (instead of me), and Rabbit has solved the code, passengers are requested to get down of the Dev-CPP Airlines, especially the latest one, since it is only this airline that does not take their route. Since otherwise, they must have tested their route in their airlines (without which they will never give an official statement releasing their plans), it was the fault of the airlines, which we regret. You may apply for a refund of your valuable money.

      We have henceforth also decided to close down this airport too.

      Thank you for your co-operation. This thread now ends hereby.

      Comment

      • HolyDoom Witch
        New Member
        • Oct 2012
        • 34

        #18
        Any closing comments?
        ***

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #19
          Any closing comments on...? There hasn't been anything else said that pertains to the question.

          Comment

          • HolyDoom Witch
            New Member
            • Oct 2012
            • 34

            #20
            Well, closing comments on how or why your code doesn't work.
            ***

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #21
              What do you mean doesn't work? It works fine for the most part. I tested it. The only fix that would be needed is an extra else if in the nested if to account for when the month is the same but the day is greater.

              But that is not the crux of the question. The point is the code works fine with nested ifs.

              Comment

              • HolyDoom Witch
                New Member
                • Oct 2012
                • 34

                #22
                "Most part" = Then it doesn't work. Plus, after a fix for THAT scenario, a fix would be needed for when the month is greater than the birth month.

                But I think the main thing that escapes your attention is that the code does not go past the FIRST nested else-if in the first place... as told in the very first post.

                Thanks for your replies, but I think the code won't work anyway, and you can see that for yourself.
                ***

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #23
                  Here is the fully working code. The only change I had to make was to add an addition else if in there to account for the situation I mentioned above. And I have tested it and it works.

                  Code:
                  	int day, month, year, b_day, b_month, b_year; 
                  	  
                  	cout << "H! " << ", what is the date today? Enter day (1 to 31): ";  
                  	cin >> day; 
                  	  
                  	cout << "Now enter the month (1 to 12): ";  
                  	cin >> month; 
                  	  
                  	cout << "And the year? (4 digits): ";  
                  	cin >> year; 
                  	  
                  	cout << "\nNow input your birth date the same way. Day: ";  
                  	cin >> b_day; 
                  	  
                  	cout << "Month: ";  
                  	cin >> b_month; 
                  	  
                  	cout << "Year: ";  
                  	cin >> b_year; 
                  	  
                  	if (month < b_month) { 
                  	   cout << "\nYour age is: " << year - b_year - 1 << " as of today on " << day << "-" << month <<"-" <<year <<"."; 
                  	} else if (month == b_month) { 
                  	   if (day < b_day) { 
                  		  cout << "\nYour age is: " << year - b_year - 1 << " as of today on " <<day <<"-" <<month <<"-" <<year <<"."; 
                  	   } else if (day == b_day) {
                  		   cout <<"\nHey... you became " <<year - b_year <<" years old today on " <<day <<"-" <<month <<"-" <<year <<"... Happy Birthday!!"; 
                  	   } else {
                  		   cout <<"\nYour age is " <<year - b_year <<" as of today on " <<day <<"-" <<month <<"-" <<year <<"."; 
                  	   }
                  	} else { 
                  	   cout <<"\nYour age is " <<year - b_year <<" as of today on " <<day <<"-" <<month <<"-" <<year <<"."; 
                  	} 
                  
                  	return 0;

                  Comment

                  • HolyDoom Witch
                    New Member
                    • Oct 2012
                    • 34

                    #24
                    I think you nailed it all right, and now I know what I was missing.

                    And I checked your code rather thoroughly, even adding one more else-if after the braces of the first one with a new condition. The program went on that line all right and printed that sentence too when that condition was fulfilled.

                    What I was missing was, that now there are TWO elses in the same program, one is in the braces that responds to the "if" which itself is IN the braces. And another one is outside, in the main code which is the FINAL ELSE. If you remember, what I was doing was, putting the final else WITHIN the braces, and then I was not left with a FINAL else to cover the rest of the situations, which of course would occur outside; this did not take my attention then. I had even written up there that all I did was put the final else in the braces (which I edited of course, when I found it was still not working fully). It was then and there, that you could have told me that 'where is the final else'? Then I would know that that "else" now belonged to the braces after it was put there. May be I needed to post the code to make that easy for you. (Otherwise... one of these days just before sleeping, I could suddenly have gotten a fit of idea that an else was missing in that code, after which I would rush to the computer from bed! That's how I remember ignored things!)

                    Other than that, since I had already given up (and closed this thread a few times), it may have been hard for me to guess that. Like you were saying you wanted to "drive" me to an answer. "Data Structures" is the last lesson I have done till now, and I haven't done enough projects of any of them. And which should be the reason for it.

                    Well it was a small thing and I ate brains of people around HERE, when what I had to do was eat MY OWN brains around THERE. Sorry about that.

                    So, thanks to everyone-- like DonBock, who may have even tried to point out my mistake. And thanks especially to Rabbit.

                    The best answer is yours; looks like no one can take that from you.

                    And yes, your code does not need the braces inside the first brace-set; there is only one action after each "if". It looks very un-confusing this way.
                    ***

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #25
                      Glad you achieved the understanding we were trying to reach. Good luck with the rest of your project.

                      Comment

                      • HolyDoom Witch
                        New Member
                        • Oct 2012
                        • 34

                        #26
                        Thanks.
                        ***
                        (Project = Studies)

                        Comment

                        Working...