Thread: Why is my if statement not working? No matter the input it always chooses N

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    7

    Why is my if statement not working? No matter the input it always chooses N

    Code:
    #include <stdio.h>int Sum();
    double ArcLength();
    int main() {
    	int result1, result2;
    	double s;
    	result1 = Sum();
    	result2 = Sum();
    	s = ArcLength();
    	printf("First sum = %d, Second sum = %d\n", result1, result2);
    	printf("Arclength = %lf meters", s);
    
    
    
    
    }
    int Sum() {
    	int a, b, c;
    	printf("Enter a value for a: ");
    	scanf("%d", &a);
    	printf("Enter a value for b: ");
    	scanf("%d", &b);
    	printf("Enter a value for c: ");
    	scanf("%d", &c);
    	//printf("The sum of a, b, and c is: %d\n", a + b + c);
    	return a + b + c;
    }
    double ArcLength() {
    	double r, AngleR, AngleD;
    	char choice;
    	printf("Input radius in meters for circle: ");
    	scanf("%lf", &r);
    	printf("Do you have an angle value in radians? (Y or N): ");
    	scanf(" %c", &choice);
    	if (choice == 'n' || 'N') {
    		printf("Enter an angle in degrees: ");
    		scanf("%lf", &AngleD);
    		printf("Your angle in radians is %lf\n", AngleD*0.017444);
    		printf("Input an angle in radians: ");
    		scanf("%lf", &AngleR);
    	}
    	else if(choice == 'y' || 'Y') {
    		printf("Input an angle in radians: ");
    		scanf("%lf", &AngleR);
    	}
    	
    	return r * AngleR;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,451
    Compound control statements require both sides of the comparison for every condition.

    Code:
    if(a < b || c < b)

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    if (choice == 'n' || 'N')
    This line doesn't compare choice to two different character constants; it first compares choice to the letter 'n', and if that fails, it then checks whether the letter 'N' exists. Since the letter 'N' does in fact exist, this statement will always be true.

    If you want to compare choice to two different letters, then you need to have a comparison with choice on both sides of the || operator.

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    7
    I get what you're trying to say to some extent, but cannot figure out how to put that into code. I'm just practicing before my test today and need to know how to do it.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    92
    Hi,

    Instead of this:
    [...]
    Quote Originally Posted by alpinanazawsze View Post
    Code:
        if (choice == 'n' || 'N') {
    
    }
    [...]

    use:

    Code:
        if(tolower(choice)=='n')

  6. #6
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    814
    Quote Originally Posted by alpinanazawsze View Post
    Code:
    if (choice == 'n' || 'N')
    Quote Originally Posted by tabstop
    it first compares choice to the letter 'n' (...) you need to have a comparison with choice on both sides of the || operator
    I get what you're trying to say to some extent, but cannot figure out how to put that into code. I'm just practicing before my test today and need to know how to do it.
    Read more carefully what was written! The comparison to 'n' does what you want, but you need to do the same on the other side of the || operator (for 'N'). Should become clear then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statement not working
    By howardbc14 in forum C Programming
    Replies: 1
    Last Post: 05-07-2015, 07:51 PM
  2. If statement not working
    By BIGDENIRO in forum C Programming
    Replies: 1
    Last Post: 10-10-2013, 02:58 PM
  3. The following if statement is not working!...
    By darkmagic in forum C Programming
    Replies: 5
    Last Post: 06-23-2010, 08:11 AM
  4. grr not working no matter what i do
    By lilhawk2892 in forum C++ Programming
    Replies: 6
    Last Post: 07-05-2006, 09:21 PM
  5. If/else statement not working
    By zenovy in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 08:26 PM

Tags for this Thread