Thread: If else confusion

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13

    If else confusion

    Greeting all,

    I have this slippery problem with an if else statement. In my if statement the code originally worked fine until I added the logical 'Or' , and now whether I enter 'y' or 'n' the code executes as if I enter a 'yes' response. All help appreciated, thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *fptr;
    
    int main()
    {
    
       char ansr;
       float amnt = 0;
       
       fptr = fopen("MOREDATA.TXT","r+"); 		 /* Opens for output */
    
    	if (fptr == 0)
    	   {printf("There was an error opening file");}
    
    	else
    
    	  {printf("Would you like to enter a beginning balance?\n");
    	      printf("Enter 'Y' for yes or 'N' for no\n");
    	        scanf(" %c", &ansr);}
    	 
    	   if (ansr == 'Y' || 'y')
    	      { printf("Enter amount - ");
    		  scanf(" %f",&amnt);
    
    		printf("Your beginning balance is %.2f\n", amnt);
    
    		fprintf(fptr,"%.2f",amnt);/* Save the amount to the disk file */
    	       }
    
    	   else
    	   /*		if (ansr == 'N' || 'n')		*/
    	      { fprintf(fptr,"1000000");
    	       }
    	       fclose(fptr);	/* Always close your files */
    
    return 0;
    }

    Still "eating the elephant one byte at a time" - author not known

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Code:
     if (ansr == 'Y' || 'y')
    Should be

    Code:
     if (ansr == 'Y' || ansr == 'y')
    A lot of people say they should be inside brackets though for better readability like below but it is personal preference.

    Code:
     if ((ansr == 'Y') || (ansr == 'y'))

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    Code:
    ansr == 'Y' || 'y'
    is always true because it is equivalent to either 0||1 or 1||1 .

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    John_

    Thanks a bunch, I did what you suggested and it works fine now!

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    peterchen

    is always true because it is equivalent to either 0||1 or 1||1 .

    not sure but it gives me more good food for thought, thanks!

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Maryland
    Posts
    13
    peterchen

    It took a couple of minutes to sink in and you are correct, sorry for the doubt, thanks again!

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    ansr == 'Y' || 'y' is same as
    (ansr == 'Y') || ('y') because of precedence

    'y' is 121 which means it is true.
    ansr == 'Y' will either be false or true depending on ansr.

    since one expression will always be true (the 'y') then it will return true.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  4. Server-net newbie confusion
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2005, 02:08 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM