Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
int main() {
    int a , b , c ,d ;
    scanf("%d %d %d %d" , &a ,&b , &c , &d);
    
    if( ( a == 1 ) || ( a == 3 ) || ( a == 5 ) )
    printf("American masculine citizen");
   
    if( ( b == 2 ) || ( b == 4 ) || ( b == 6 ) )
    printf("American feminine citizen");
   
    if (c==7)
    printf("Foreign masculine citizen");
    
    if (d==8):
    printf("Foreign feminine citizen");
        else
        printf("Invalid value");

    return 0;
}


My program is displaying me "American masculine citizenInvalid value" instead of just "American masculine citizen"

What I have tried:

i tried to sepparate the if s with commas , also searched the net but found only for phyton results . i dont understand why is printing like this
Posted
Updated 10-Nov-22 6:01am
Comments
jeron1 10-Nov-22 11:34am    
You are executing every if statement, eventually you always get to the last one, if d is not equal to 8, you will perform the else statement, and print "Invalid value". You could just return after printing "American masculine citizen", "American feminine citizen", or "Foreign masculine citizen". There are several other ways to handle this as well.

Why are you using different variables? You only need one value to test:
C++
#include <stdio.h>
int main() {
    int citizen;
    scanf("%d" , &citizen);
    
    if( ( citizen == 1 ) || ( citizen == 3 ) || ( citizen == 5 ) )
        printf("American masculine citizen");
   
    else if( ( citizen == 2 ) || ( citizen == 4 ) || ( citizen == 6 ) )
        printf("American feminine citizen");
   
    else if (citizen==7)
        printf("Foreign masculine citizen");
    
    else if (citizen==8):
        printf("Foreign feminine citizen");
    else
        printf("Invalid value");

    return 0;
}
 
Share this answer
 
Comments
Bogdan Alexandru Oct2022 10-Nov-22 12:37pm    
better.Thanks
If your conditional expressions are exclusive which means only one condition can be true then you need to use else statements with the ifs. That would look like this :
C
#include <stdio.h>
int main() {
    int a , b , c ,d ;
    scanf("%d %d %d %d" , &a ,&b , &c , &d);
    
    if( ( a == 1 ) || ( a == 3 ) || ( a == 5 ) )
        printf("American masculine citizen");
    else if( ( b == 2 ) || ( b == 4 ) || ( b == 6 ) )
        printf("American feminine citizen");
    else if (c==7)
        printf("Foreign masculine citizen");
    else if (d==8)    // : <- the colon there is not valid
        printf("Foreign feminine citizen");
    else
        printf("Invalid value");

    return 0;
}
 
Share this answer
 
Comments
Bogdan Alexandru Oct2022 10-Nov-22 12:37pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900