Thread: if problem

  1. #1
    Registered User
    Join Date
    Sep 2011

    if problem

    Problem is when i press B as the center element the line at the end "if(centerelement=="B") should run what inside.

    I put this inside the if statement:
    VEC = 3;
    I put the printf so i can verify that the value of VEC is now 3.

    Here is the source code:

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    	char charselect[1];
    	int select;
    	char tempelement[3];
    	char centerelement[3];
    	char pelement1[3]; //periferal element 1
    	char pelement2[3]; //periferal element 2
    	char pelement3[3]; //periferal element 3
    	char pelement4[3]; //periferal element 4
    	int totalVE; //total valance electrons
    	int VEC; //# of valance electrons in center
    	int VE1;
    	int VE2;
    	int VE3;
    	int VE4;
    /*	H = 1;
    	B = 3;
    	C = 4;
    	N = 5;
    	P = 5;
    	O = 6;
    	S = 6;
    	Se = 6;
    	F = 7;
    	Cl = 7;
    	Br = 7;
    	I = 7;
    	printf("What is the structure of the Lewis Structure?\n\n");
    	printf("1 - Tetrahedral\n\n");
    	printf("Input corresponding number: ");
    	gets (charselect);
    	select = atoi(charselect);
    	if (select == 1)
    		printf("You selected Tetrahedral.\n\n");
    		printf("What is the center Element? \n");
    		printf("What is the 1st periferal element? \n");
    		printf("What is the 2nd periferal element? \n");
    		printf("What is the 3rd periferal element? \n");
    		printf("What is the 4th periferal element? \n");
    			VEC = 3;
    		printf("%s %s %s %s %s", centerelement, pelement1, pelement1, pelement1, pelement1);

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    You can't compare strings with the == operator. Try using strcmp. It returns 0 when the strings are identical.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Ah, this is what I get for reading only half of the book, being impatient/excited to try out something by myself...
    I put:
    int strcmp(centerelement,"B");
    But getting an error, what is wrong with what I put? I did "#include <string.h>" on top.
    Nevermind, The example given was bad. I removed int from strcmp and put in "int compare;" then "compare = strcmp(centerelement,"B");"

    Fixed it, thanks
    Last edited by JonathanS; 09-01-2011 at 08:41 AM.

  4. #4
    Join Date
    Aug 2010
    Ontario Canada
    No need for an intermediate variable, you can just do this...
    //  old way... if(centerelement=="B")
    if (! strcmp(centerelement, "B"))
           VEC = 3;
    I generally advise that you always look for ways to simplify your code, never using anything that's not entirely necessary to get the job done...
    Last edited by CommonTater; 09-01-2011 at 10:06 AM.

  5. #5
    Join Date
    Dec 2007
    First off...stop using gets()! It's insecure; view the FAQ here for why and how to fix it.

    Secondly, make charselect a single char variable, rather than an array of length 1 (which is wrong, because then there's no room for the required null terminator).

    Example code:

    #include <stdio.h>
    int main(void)
       char selection;
       int c = 0;
       printf("1 - Tetrahedral\n");
       printf("Select one: ");
       while (scanf("%c", &selection) != 1) {
          while((c = getchar()) != '\n') ; /* Consume any leftover chars in the buffer */
          printf("Invalid selection!\nSelect one: ");
       while((c = getchar()) != '\n') ; /* Consume any leftover chars in the buffer */
       if (selection == '1') {
          printf("You selected %c\n", selection);
       } else {
          printf("You selected %c, which is invalid!\n", selection);
       return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM
Website Security Test