Thread: While loop not checking if VE>0

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    While loop not checking if VE>0

    Having a problem with SO4(charge +2). I know it isn't a real compound, I just did it to test if it takes away lone pairs correctly.

    All of the code for this is in the int put_lone_pairs() function.

    The real element is SO4(-2), that shows fine. Every time the charge goes up 2, it should remove two dots or one "~", which represents a lone pair.

    SO4(0), works fine, it takes away one lone pair, but when I do anything +2 or above is just doesn't work. I looked at the code and it should check every time a lone pair is put on if the amount of valance elections and maxC/1/2/3/4 (the maximum electrons an element can hold is greater than 0, so more electrons are not put on than possible (can't put 4 electrons if you only have 2).

    There is something I am missing here obviously, thanks for your help

    P.S. I know there are a lot of global variables, and I'll fix it after this problem. Or... is it the problem?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    //make the program ask how many perifers are around it, get rid of tetrahedral/groups
    //allow mutiple view latter on, i.e. orbitals, lewis structure, etc
    
    //in order to be able to show isomers, you put all of lone pairs of same atoms = to each other, 
    //which ever is greater you switch the extras between the same atom
    
    //*********If electrons are left over after filling up all lone pairs it still shows everything is fine. Move "finished making model" line  model after it checks that 
    
    all electrons are used (=0)
    //******** also if it goes under for some reason it fills the lone pairs no matter what
    
    
    	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 i; //counter
    
    	int charge; //input charge of molecule
    	char chargestr[3];
    
    	int VEC; //# of valance electrons in element
    	int VE1;
    	int VE2;
    	int VE3;
    	int VE4;
    	int totalVE; //total valance electrons
    	int VE; //VE avaliable to work with in model
    	int VEtemp;
    
    	int compare; //used to compare two strings, if = 0, then they are the same string. i.e. C=C or Cl=Cl
    
    //max bonds & lonepairs an atom can have
    	int maxC;
    	int max1;
    	int max2;
    	int max3;
    	int max4;
    	int temp_max;
    
    
    //LONE PAIRS set to empty as default
    	char a1[3] = "  ";
    	char a2[3] = "  ";
    	char a3[3] = "  ";
    	char b1[3] = "  ";
    	char b2[3] = "  ";
    	char b3[3] = "  ";
    	char c1[3] = "  ";
    	char c2[3] = "  ";
    	char c3[3] = "  ";
    	char d1[3] = "  ";
    	char d2[3] = "  ";
    	char d3[3] = "  ";
    
    
    
    /*********************************************************************************/
    /***************************Declaration of Functions******************************/
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    
    int calc_valance(char* tempElement);
    int calc_max_valance(char* tempElement);
    int put_lone_pairs(int max1, int max2, int max3, int max4, int VE);
    
    
    /*********************************************************************************/
    /*********************************************************************************/
    /*******************************Start of program**********************************/
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    
    int main()
    
    
    {
    	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? ");
    		scanf("%s", tempElement);
    		strcpy (centerelement, tempElement);
    		calc_valance(tempElement);		
    		VEC = VEtemp;
    		calc_max_valance(tempElement);
    		maxC = temp_max;
    
    		printf("What is the 1st periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement1, tempElement);
    		calc_valance(tempElement);
    		VE1 = VEtemp;
    		calc_max_valance(tempElement);
    		max1 = temp_max;
    
    		printf("What is the 2nd periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement2, tempElement);
    		calc_valance(tempElement);
    		VE2 = VEtemp;
    		calc_max_valance(tempElement);
    		max2 = temp_max;
    
    		printf("What is the 3rd periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement3, tempElement);
    		calc_valance(tempElement);
    		VE3 = VEtemp;
    		calc_max_valance(tempElement);
    		max3 = temp_max;
    
    		printf("What is the 4th periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement4, tempElement);
    		calc_valance(tempElement);
    		VE4 = VEtemp;
    		calc_max_valance(tempElement);
    		max4 = temp_max;
    
    
    		printf("What is the charge of the molecule?");
           		scanf("%d", &charge);
            	printf("\nThe charge of the molecule is %d", charge);
    
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    
    
    		totalVE = VEC + VE1 + VE2 + VE3 + VE4;
    		VE = totalVE - charge; //calculates valence electrons left with charge given
    		
            	printf("\n\nTotal available Electrongs available after charge = %d\n\n", VE);
    
    
    //Put single bonds on periferals
    
    		VE = VE - 8;
    		maxC = maxC - 8;
    		max1 = max1 - 2;
    		max2 = max2 - 2;
    		max3 = max3 -2;
    		max4 = max4 -2;
    
    		//no way to check for error yet for max
    		
    		printf("After putting single bonds on all connections, %d electrons remain to be used\n\n", VE);
    
    		//need to make it so that it does one lone pair at a time for each periferal, 
           		//instead of dumping it all on one, and when filled go to the next one
    
    		//later on put charges on atoms. i.e. (+) to replace a missing pair of electrons
    
    		VE = put_lone_pairs(max1, max2, max3, max4, VE);
    
    		//for some reason doesn't return the negated VE
    
    
    			printf("~ = .. = Lone Pair\n\n");
    			printf("            %s\n", a2);
    			printf("          %s%s%s\n", a1, pelement1, a3);
    			printf("    	    |\n");
    			printf("    	    |\n");
    			printf("    	    |\n");
    			printf("	    %s\n", centerelement);
    			printf("           /|\\\n");
    			printf("          / | \\\n");
    			printf("         /  |  \\\n");
    			printf("        /   |   \\\n");
    			printf("       /    |    \\\n");
    			printf("      /     |     \\\n");
    			printf("  %s%s%s   %s%s%s  %s%s%s\n", b2, pelement2, b3, c2, pelement3, c3, d2, pelement4, d3);
    			printf("    %s       %s      %s\n\n", b1, c1, d1);
    
    			//make it check if it used all electrons. If it is less than 0 or more than 0 it should err
    			if(VE>0)
    				printf("Error! There are %d Valance Electons left to be used! Model is incorrect!\n", VE);
    
    			else if (VE<0)
    				printf("Error! There were more electrons used than available! Model is incorrect!\n");
    	
    			else
    				printf("Finished making model.\n");
    
    		
    
    	} //end of tetrahedral (if select = 1)
    
    	else //if incorrect input for shape of molecule was chosen, error occurs
        {
            printf("Error, no selection has been made.");
        }
    
    	return(0);
    
    } //end main
    
    
    int calc_valance(char* tempElement)
    {
    
    		compare = strcmp(tempElement,"H");
    		
    		if(compare==0)
    		{
    			VEtemp = 1;
    		}
    
    		compare = strcmp(tempElement,"B");
    		
    		if(compare==0)
    		{
    			VEtemp = 3;
    		}
    
    		compare = strcmp(tempElement,"C");
    
    		if(compare==0)
    		{
    			VEtemp = 4;
    		}
    
    		
    		compare = strcmp(tempElement,"N");
    
    		if(compare==0)
    		{
    			VEtemp = 5;
    		}
    
    		compare = strcmp(tempElement,"P");
    
    		if(compare==0)
    		{
    			VEtemp = 5;
    		}
    
    		compare = strcmp(tempElement,"O");
    
    		if(compare==0)
    		{
    			VEtemp = 6;
    		}
    
    
    		compare = strcmp(tempElement,"S");
    
    		if(compare==0)
    		{
    			VEtemp = 6;
    		}
    
    		compare = strcmp(tempElement,"Se");
    
    		if(compare==0)
    		{
    			VEtemp = 6;
    		}
    
    		compare = strcmp(tempElement,"F");
    
    		if(compare==0)
    		{
    			VEtemp = 7;
    		}
    
    		compare = strcmp(tempElement,"Cl");
    
    		if(compare==0)
    		{
    			VEtemp = 7;
    		}
    
    		compare = strcmp(tempElement,"Br");
    
    		if(compare==0)
    		{
    			VEtemp = 7;
    		}
    
    		compare = strcmp(tempElement,"I");
    
    		if(compare==0)
    		{
    			VEtemp = 7;
    		}
    
    
    		return(VEtemp);
    
    }//end calc_valance
    
    
    int put_lone_pairs(int max1, int max2, int max3, int max4, int VE)
    {
    	while(max1>0 || max2>0 || max3>0 || max4>0 && VE>0)
    	{
    		compare = strcmp(a1,"  ");
    		if (compare==0)
    		{
    			strcpy (a1,"..");
    			max1 = max1 - 2;
    			VE = VE - 2;
    			continue;
    
    		}
    
    		compare = strcmp(b1,"  ");
    		if (compare==0)
    		{
    			strcpy (b1,"~");
    			max2 = max2 - 2;
    			VE = VE - 2;
    			continue;
    		}
    
    		compare = strcmp(c1,"  ");
    		if (compare==0)
    		{
    			strcpy (c1,"~");
    			max3 = max3 - 2;
    			VE = VE -2;
    			continue;
    		}
    		compare = strcmp(d1,"  ");
    		if (compare==0)
    		{
    			strcpy (d1,"~");
    			max4 = max4 - 2;
    			VE = VE -2;
    			continue;
    		}
    		
    		compare = strcmp(a2,"  ");
    		if (compare==0)
    		{
    			strcpy (a2,"~");
    			max1 = max1 - 2;
    			VE = VE -2;
    			continue;
    		}
    
    		compare = strcmp(b2,"  ");
    		if (compare==0)
    		{
    			strcpy (b2,"..");
    			max2 = max2 - 2;
    			VE = VE -2;
    			continue;
    		}
    
    		compare = strcmp(c2,"  ");
    		if (compare==0)
    		{
    			strcpy (c2,"..");
    			max3 = max3 - 2;
    			VE = VE -2;
    			continue;
    		}
    		compare = strcmp(d2,"  ");
    		if (compare==0)
    		{
    			strcpy (d2,"..");
    			max4 = max4 - 2;
    			VE = VE -2;
    			continue;
    		}
    		
    		compare = strcmp(a3,"  ");
    		if (compare==0)
    		{
    			strcpy (a3,"..");
    			max1 = max1 - 2;
    			VE = VE -2;
    			continue;
    		}
    
    		compare = strcmp(b3,"  ");
    		if (compare==0)
    		{
    			strcpy (b3,"..");
    			max2 = max2 - 2;
    			VE = VE -2;
    			continue;
    		}
    
    		compare = strcmp(c3,"  ");
    		if (compare==0)
    		{
    			strcpy (c3,"..");
    			max3 = max3 - 2;
    			VE = VE -2;
    			continue;
    		}
    		compare = strcmp(d3,"  ");
    		if (compare==0)
    		{
    			strcpy (d3,"..");
    			max4 = max4 - 2;
    			VE = VE -2;
    			continue;
    		}
    	}
    		printf("After putting lone pairs, %d electrons remain\n\n", VE);
    		return(VE);
    
    }//end put_lone_pairs
    
    
    int calc_max_valance(char* tempElement)
    {		
    		if(strcmp(tempElement, "H") == 0)
    			temp_max = 2;
    		else if (strcmp(tempElement, "B") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "C") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "N") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "P") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "O") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "S") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "Se") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "F") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "Cl") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "Br") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "I") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "F") == 0)
    			temp_max = 8;
    		else
    			printf("Error, Element not found in database.");
    
    		return(temp_max);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > gets (charselect);
    Well aside from the dangers of using gets(), a char[1] array can only hold \0.
    Otherwise you have a buffer overrun.

    I'd check ALL your other very short strings as well for similar issues.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Fixed those problems. Still bothering the hell out of me. I tried changing all the OR's to AND's (|| to &&) in the put_lone_pairs() function... no luck.

    going to try putting all the unnecessary global variables into the appropriate functions, see if that helps anything... :/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you posted some example input, we might be able to point you in the right direction.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by JonathanS View Post
    I tried changing all the OR's to AND's (|| to &&) ... no luck.
    I know when I'm stuck on a problem, one of the first things I try is switching all of my ORs to ANDs to see if that fixes it!


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    I know when I'm stuck on a problem, one of the first things I try is switching all of my ORs to ANDs to see if that fixes it!


    Quzah.
    I always go the CTRL-A then DEL route myself.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Quote Originally Posted by Salem View Post
    Well if you posted some example input, we might be able to point you in the right direction.
    Thanks for letting me know, I thought I explained the problem, but obviously didn't.

    Here is the output, as you can see, the only inputs I am changing it the charge. From -2, to 0, to +2. Everytime the charge goes up 2 an electron pait should be taken off. As you can see with the example of the 0 charge compared to the -2 charge. The -2 charge has 3 lone pairs on every peripheral element.

    The 0 charge has one missing on only one. The +2 charge shows the same thing as the 0 charge BUT shows an error because of the error checking I put in place.

    Hope that clears things up :S

    Code:
    c:\prog1>LS
    What is the structure of the Lewis Structure?
    
    1 - Tetrahedral
    
    Input corresponding number: 1
    You selected Tetrahedral.
    
    What is the center Element? S
    What is the 1st periferal element? O
    What is the 2nd periferal element? O
    What is the 3rd periferal element? O
    What is the 4th periferal element? O
    What is the charge of the molecule?-2
    
    The charge of the molecule is -2
    
    Total available Electrongs available after charge = 32
    
    After putting single bonds on all connections, 24 electrons remain to be used
    
    After putting lone pairs, 0 electrons remain
    
    ~ = .. = Lone Pair
    
                ~
              ..O..
                |
                |
                |
                S
               /|\
              / | \
             /  |  \
            /   |   \
           /    |    \
          /     |     \
      ..O..   ..O..  ..O..
        ~       ~      ~
    
    Finished making model.
    
    c:\prog1>LS
    What is the structure of the Lewis Structure?
    
    1 - Tetrahedral
    
    Input corresponding number: 1
    You selected Tetrahedral.
    
    What is the center Element? S
    What is the 1st periferal element? O
    What is the 2nd periferal element? O
    What is the 3rd periferal element? O
    What is the 4th periferal element? O
    What is the charge of the molecule?0
    
    The charge of the molecule is 0
    
    Total available Electrongs available after charge = 30
    
    After putting single bonds on all connections, 22 electrons remain to be used
    
    After putting lone pairs, 0 electrons remain
    
    ~ = .. = Lone Pair
    
                ~
              ..O..
                |
                |
                |
                S
               /|\
              / | \
             /  |  \
            /   |   \
           /    |    \
          /     |     \
      ..O..   ..O..  ..O
        ~       ~      ~
    
    Finished making model.
    
    
    c:\prog1>LS
    What is the structure of the Lewis Structure?
    
    1 - Tetrahedral
    
    Input corresponding number: 1
    You selected Tetrahedral.
    
    What is the center Element? S
    What is the 1st periferal element? O
    What is the 2nd periferal element? O
    What is the 3rd periferal element? O
    What is the 4th periferal element? O
    What is the charge of the molecule?+2
    
    The charge of the molecule is 2
    
    Total available Electrongs available after charge = 28
    
    After putting single bonds on all connections, 20 electrons remain to be used
    
    After putting lone pairs, -2 electrons remain
    
    ~ = .. = Lone Pair
    
                ~
              ..O..
                |
                |
                |
                S
               /|\
              / | \
             /  |  \
            /   |   \
           /    |    \
          /     |     \
      ..O..   ..O..  ..O
        ~       ~      ~
    
    Error! There were more electrons used than available! Model is incorrect!
    Edit: and changing the OR's to AND's actually messed it up more. Reason being is that if it does and then when one peripheral element gets 3 lone pairs then the While fails and skips the rest of the peripherals who "want" another lone pair.
    Last edited by JonathanS; 09-28-2011 at 08:45 AM. Reason: Forgot to reply on quzah's post

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    problem fixed.... teh problem now is i have no clue why XD

    Can someone please explain it to me? I put an If before the while with the SAME statements, problem fixed...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    //make not output visuals from comnd promp, have a sperate window
    //make the program ask how many perifers are around it, get rid of tetrahedral/groups
    //allow mutiple view latter on, i.e. orbitals, lewis structure, etc
    
    //in order to be able to show isomers, you put all of lone pairs of same atoms = to each other, 
    //which ever is greater you switch the extras between the same atom
    
    //*********If electrons are left over after filling up all lone pairs it still shows everything is fine. Move "finished making model" line  model after it checks that //all electrons are used (=0)
    //******** also if it goes under for some reason it fills the lone pairs no matter what
    
    
    	char charselect[2];
    	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 i; //counter
    
    	int charge; //input charge of molecule
    	char chargestr[3];
    
    	int VEC; //# of valance electrons in element
    	int VE1;
    	int VE2;
    	int VE3;
    	int VE4;
    	int totalVE; //total valance electrons
    	int VE; //VE avaliable to work with in model
    	int VEtemp;
    
    	int compare; //used to compare two strings, if = 0, then they are the same string. i.e. C=C or Cl=Cl
    
    //max bonds & lonepairs an atom can have
    	int maxC;
    	int max1;
    	int max2;
    	int max3;
    	int max4;
    	int temp_max;
    
    
    //LONE PAIRS set to empty as default
    	char a1[3] = "  ";
    	char a2[3] = "  ";
    	char a3[3] = "  ";
    	char b1[3] = "  ";
    	char b2[3] = "  ";
    	char b3[3] = "  ";
    	char c1[3] = "  ";
    	char c2[3] = "  ";
    	char c3[3] = "  ";
    	char d1[3] = "  ";
    	char d2[3] = "  ";
    	char d3[3] = "  ";
    
    
    
    /*********************************************************************************/
    /***************************Declaration of Functions******************************/
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    
    int calc_valance(char* tempElement);
    int calc_max_valance(char* tempElement);
    int put_lone_pairs(int max1, int max2, int max3, int max4, int VE);
    
    
    /*********************************************************************************/
    /*********************************************************************************/
    /*******************************Start of program**********************************/
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    /*********************************************************************************/
    
    int main()
    
    
    {
    	printf("What is the structure of the Lewis Structure?\n\n");
    	printf("1 - Tetrahedral\n\n");
    	printf("Input corresponding number: ");
    	scanf("%s", charselect);
    	select = atoi(charselect);
    
    	if (select == 1)
    	{
    		printf("You selected Tetrahedral.\n\n");
    
    		printf("What is the center Element? ");
    		scanf("%s", tempElement);
    		strcpy (centerelement, tempElement);
    		calc_valance(tempElement);		
    		VEC = VEtemp;
    		calc_max_valance(tempElement);
    		maxC = temp_max;
    
    		printf("What is the 1st periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement1, tempElement);
    		calc_valance(tempElement);
    		VE1 = VEtemp;
    		calc_max_valance(tempElement);
    		max1 = temp_max;
    
    		printf("What is the 2nd periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement2, tempElement);
    		calc_valance(tempElement);
    		VE2 = VEtemp;
    		calc_max_valance(tempElement);
    		max2 = temp_max;
    
    		printf("What is the 3rd periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement3, tempElement);
    		calc_valance(tempElement);
    		VE3 = VEtemp;
    		calc_max_valance(tempElement);
    		max3 = temp_max;
    
    		printf("What is the 4th periferal element? ");
    		scanf("%s", tempElement);
    		strcpy (pelement4, tempElement);
    		calc_valance(tempElement);
    		VE4 = VEtemp;
    		calc_max_valance(tempElement);
    		max4 = temp_max;
    
    		printf("What is the charge of the molecule?");
           		scanf("%d", &charge);
            	printf("\nThe charge of the molecule is %d", charge);
    
    
    		totalVE = VEC + VE1 + VE2 + VE3 + VE4;
    		VE = totalVE - charge; //calculates valence electrons left with charge given
    		
            	printf("\n\nTotal available Electrongs available after charge = %d\n\n", VE);
    
    
    //Put single bonds on periferals
    
    		VE = VE - 8;
    		maxC = maxC - 8;
    		max1 = max1 - 2;
    		max2 = max2 - 2;
    		max3 = max3 -2;
    		max4 = max4 -2;
    
    		//no way to check for error yet for max
    		
    		printf("After putting single bonds on all connections, %d electrons remain to be used\n\n", VE);
    
    		//need to make it so that it does one lone pair at a time for each periferal, 
           		//instead of dumping it all on one, and when filled go to the next one
    
    		//later on put charges on atoms. i.e. (+) to replace a missing pair of electrons
    
    		VE = put_lone_pairs(max1, max2, max3, max4, VE);
    
    		//for some reason doesn't return the negated VE
    
    
    			printf("~ = .. = Lone Pair\n\n");
    			printf("            %s\n", a2);
    			printf("          %s%s%s\n", a1, pelement1, a3);
    			printf("    	    |\n");
    			printf("    	    |\n");
    			printf("    	    |\n");
    			printf("	    %s\n", centerelement);
    			printf("           /|\\\n");
    			printf("          / | \\\n");
    			printf("         /  |  \\\n");
    			printf("        /   |   \\\n");
    			printf("       /    |    \\\n");
    			printf("      /     |     \\\n");
    			printf("  %s%s%s   %s%s%s  %s%s%s\n", b2, pelement2, b3, c2, pelement3, c3, d2, pelement4, d3);
    			printf("    %s       %s      %s\n\n", b1, c1, d1);
    
    			//make it check if it used all electrons. If it is less than 0 or more than 0 it should err
    			if(VE>0)
    				printf("Error! There are %d Valance Electons left to be used! Model is incorrect!\n", VE);
    
    			else if (VE<0)
    				printf("Error! There were more electrons used than available! Model is incorrect!\n");
    	
    			else
    				printf("Finished making model.\n");
    
    		
    
    	} //end of tetrahedral (if select = 1)
    
    	else //if incorrect input for shape of molecule was chosen, error occurs
        {
            printf("Error, no selection has been made.");
        }
    
    	return(0);
    
    } //end main
    
    
    int calc_valance(char* tempElement)
    {
    //maybe not put an error check twice bc it owuld show twice, also i do not think it shows at all
    
    		if(strcmp(tempElement, "H") == 0)
    			VEtemp = 1;
    		else if(strcmp(tempElement, "B") == 0)
    			VEtemp = 3;
    		else if(strcmp(tempElement, "C") == 0)
    			VEtemp = 4;
    		else if(strcmp(tempElement, "N") == 0)
    			VEtemp = 5;
    		else if(strcmp(tempElement, "P") == 0)
    			VEtemp = 5;
    		else if(strcmp(tempElement, "O") == 0)
    			VEtemp = 6;
    		else if(strcmp(tempElement, "S") == 0)
    			VEtemp = 6;
    		else if(strcmp(tempElement, "Se") == 0)
    			VEtemp = 6;
    		else if(strcmp(tempElement, "F") == 0)
    			VEtemp = 7;
    		else if(strcmp(tempElement, "Cl") == 0)
    			VEtemp = 7;
    		else if(strcmp(tempElement, "Br") == 0)
    			VEtemp = 7;
    		else if(strcmp(tempElement, "I") == 0)
    			VEtemp = 7;
    		else
    			printf("Error, Element not found in database.");
    
    		return(VEtemp);
    
    }//end calc_valance
    
    
    int put_lone_pairs(int max1, int max2, int max3, int max4, int VE)
    {
    
    	//int tempMax1 = max1;
    	//int tempMax2 = max2;
    	//int tempMax3 = max3;
    	//int tempMax4 = max4;
    	//int tempVE = VE;
    
    	if (VE>=0 && max1>=2 || max2>=2 || max3>=2 || max4>=2)
    	{
    	while(VE>0)
    	{
    		compare = strcmp(a1,"  ");
    		if (compare==0)
    		{
    			strcpy (a1,"..");
    			max1 = max1 - 2;
    			VE = VE - 2;
    			printf("VE = %d, max1 = %d\n", VE, max1);
    			continue;
    
    		}
    
    		compare = strcmp(b1,"  ");
    		if (compare==0)
    		{
    			strcpy (b1,"~");
    			max2 = max2 - 2;
    			VE = VE - 2;
    			printf("VE = %d, max2 = %d\n", VE, max2);
    			continue;
    		}
    
    		compare = strcmp(c1,"  ");
    		if (compare==0)
    		{
    			strcpy (c1,"~");
    			max3 = max3 - 2;
    			VE = VE -2;
    			printf("VE = %d, max4 = %d\n", VE, max3);
    			continue;
    		}
    		compare = strcmp(d1,"  ");
    		if (compare==0)
    		{
    			strcpy (d1,"~");
    			max4 = max4 - 2;
    			VE = VE -2;
    			printf("VE = %d, max4 = %d\n", VE, max4);
    			continue;
    		}
    		
    		compare = strcmp(a2,"  ");
    		if (compare==0)
    		{
    			strcpy (a2,"~");
    			max1 = max1 - 2;
    			VE = VE -2;
    			printf("VE = %d, max1 = %d\n", VE, max1);
    			continue;
    		}
    
    		compare = strcmp(b2,"  ");
    		if (compare==0)
    		{
    			strcpy (b2,"..");
    			max2 = max2 - 2;
    			VE = VE -2;
    			printf("VE = %d, max2 = %d\n", VE, max2);
    			continue;
    		}
    
    		compare = strcmp(c2,"  ");
    		if (compare==0)
    		{
    			strcpy (c2,"..");
    			max3 = max3 - 2;
    			VE = VE -2;
    			printf("VE = %d, max3 = %d\n", VE, max3);
    			continue;
    		}
    		compare = strcmp(d2,"  ");
    		if (compare==0)
    		{
    			strcpy (d2,"..");
    			max4 = max4 - 2;
    			VE = VE -2;
    			printf("VE = %d, max4 = %d\n", VE, max4);
    			continue;
    		}
    		
    		compare = strcmp(a3,"  ");
    		if (compare==0)
    		{
    			strcpy (a3,"..");
    			max1 = max1 - 2;
    			VE = VE -2;
    			printf("VE = %d, max1 = %d\n", VE, max1);
    			continue;
    		}
    
    		compare = strcmp(b3,"  ");
    		if (compare==0)
    		{
    			strcpy (b3,"..");
    			max2 = max2 - 2;
    			VE = VE -2;
    			printf("VE = %d, max2 = %d\n", VE, max2);
    			continue;
    		}
    
    		compare = strcmp(c3,"  ");
    		if (compare==0)
    		{
    			strcpy (c3,"..");
    			max3 = max3 - 2;
    			VE = VE -2;
    			printf("VE = %d, max3 = %d\n", VE, max3);
    			continue;
    		}
    		compare = strcmp(d3,"  ");
    		if (compare==0)
    		{
    			strcpy (d3,"..");
    			max4 = max4 - 2;
    			VE = VE -2;
    			printf("VE = %d, max4 = %d\n", VE, max4);
    			continue;
    		}
    	}
    }
    		printf("After putting lone pairs, %d electrons remain\n\n", VE);
    		return(VE);
    
    }//end put_lone_pairs
    
    
    int calc_max_valance(char* tempElement)
    {		
    		if(strcmp(tempElement, "H") == 0)
    			temp_max = 2;
    		else if (strcmp(tempElement, "B") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "C") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "N") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "P") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "O") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "S") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "Se") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "F") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "Cl") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "Br") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "I") == 0)
    			temp_max = 8;
    		else if (strcmp(tempElement, "F") == 0)
    			temp_max = 8;
    		else
    			printf("Error, Element not found in database.");
    
    		return(temp_max);
    }

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    now that i look at it, it is even more strange... why isn't it outputting the "printf("VE = %d, max4 = %d\n", VE, max3);" yet doing the correct calculations? This is definitely some wizardry going around in here....

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    nvm it shows

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I get the same results whether you have your if statement or not.

    For what it's worth, the code has a big problem here.
    Code:
        while(VE>0)
        {
            compare = strcmp(a1,"  ");
            if (compare==0)
            {
                strcpy (a1,"..");
                max1 = max1 - 2;
                VE = VE - 2;
                printf("VE = %d, max1 = %d\n", VE, max1);
                continue;
            }
            // more of the same
    
            // add this line at the end
            break;
        }
        printf("After putting lone pairs, %d electrons remain\n\n", VE);
    The problem is if NONE of your tests change VE, then the code gets stuck in an infinite loop.
    The blue code will at least mean that if none of the conditions modify VE, then it won't get stuck, and you can print your message that say 1 electron remains.

    Also, you really need to do something about all those global variables.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Righto, I actually changed all of them in another file, I'll play around with those and see if it works (I also added the break file there).

    I'll look it up more next weekend, I got a lot of actual studying to do XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  2. Replies: 5
    Last Post: 02-09-2010, 06:08 PM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  5. Error checking with loop
    By CrackerJack in forum C Programming
    Replies: 4
    Last Post: 10-22-2003, 07:37 PM