Beginner: Problem with If-Else Statement

This is a discussion on Beginner: Problem with If-Else Statement within the C Programming forums, part of the General Programming Boards category; My assignment was to write a program to ask for the user's name and if they were a U.S. citizen. ...

  1. #1
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67

    Beginner: Problem with If-Else Statement

    My assignment was to write a program to ask for the user's name and if they were a U.S. citizen. If they are, ask them for the state they are from and input the data in a union. If they are not, ask what country they are from and input the data into the same union.

    I have written my code, but the If-Else statement in my DataEnter() function doesn't seem to be working properly. It keeps evaluating to "true" no matter what I type in. Any help would be appreciated. Below is the code I have written:

    Code:
    #include <stdio.h>
    
    struct bit_field {
    	int us_cit: 1;
    	int non_us_cit: 1;
    };
    
    struct personal {
    	char name[20];
    	struct bit_field citizen; 
    	union {
    		char state[20];
    		char country[20];
    	} nationality;	
    } info;
    
    typedef struct personal PER;
    
    void DataEnter(PER *ptr);
    void DataDisplay(PER *ptr);
    
    int main(void)
    {
    	DataEnter(&info);
    	DataDisplay(&info);
    	
    	return 0;
    }
    
    // DataEnter() definition
    void DataEnter(PER *ptr)
    {
    	char is_yes[4];
    	
    	printf("Please enter your name:\n");
    		gets(ptr->name);
    	printf("Are you a U.S. citizen: (Yes or No)\n");
    		gets(is_yes);
    	if ((is_yes[0] == 'Y') || (is_yes[0] = 'y')) {		
    		printf("Please enter the name of the state you are from:\n");
    		gets(ptr->nationality.state);
    		ptr->citizen.us_cit = 1;
    		ptr->citizen.non_us_cit = 0;
    	}
    	else {
    		printf("Please enter the name of the country you are from:\n");
    		gets(ptr->nationality.country);
    		ptr->citizen.non_us_cit = 1;
    		ptr->citizen.us_cit = 0;
    	}
    }
    
    // DataDisplay() definition
    void DataDisplay(PER *ptr)
    {
    	printf("\nBelow is a list of the information you have entered:\n");
    	printf("Name: %s\n", ptr->name);
    	if (ptr->citizen.us_cit && !ptr->citizen.non_us_cit) 
    		printf("State: %s\n", ptr->nationality.state);
    	else if (!ptr->citizen.us_cit && ptr->citizen.non_us_cit)
    		printf("Country: %s\n", ptr->nationality.country);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    =
    ==
    not the same.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    if ((is_yes[0] == 'Y') || (is_yes[0] = 'y')) {
    Edit::- soundly beaten by Tabstop! (Stupid netbook...)

    @ the OP - you should look at using a different method than gets to get your input. See the FAQ for some more details:

    Why gets() is bad...
    C-FAQ 12.23 - Why does everyone say not to use gets()?
    Last edited by kermit; 08-16-2009 at 08:37 PM.

  4. #4
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    omg......FAIL because of a typo *embarrased*

    Thank you for the replies guys, I appreciate the help, and thank you for the links Kermit, I will be sure to read them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. Problem with extremely beginner exercise
    By Molokai in forum C++ Programming
    Replies: 11
    Last Post: 05-08-2007, 09:05 AM
  4. begginers if statement problem
    By lastresort in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2005, 03:56 PM
  5. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21