Thread: ; after while()? wth?

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

    ; after while()? wth?

    Ok, so the only way to make this work is to put ; at the end of line 63... Why? It is a ELSE :/

    Also it doesn't solve the problem I am having. It loops after the IF. Now it doesnt even write the CHHHH to disk, only NHHHH's.

    It should write CHHHH to disk bc of the if in void write_File(char* randString)... :/

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //Different Combos = H B C N P O S Se F Cl Br I F
    
    char randString[20] = "";
    int finish = 0;
    
    
    
    void write_File(char* randString);
    char* random(char* randString, int finish);
    
    
    int main()
    {
    
    	while(finish != 1)
    	{
    		strcpy(randString, random(randString, finish));
    		write_File(randString);
    	}
    
    	return(0);
    
    } //END MAIN
    
    
    
    
    void write_File(char* randString)
    {
    
    	FILE* fts;
    
    	fts = fopen("data.dat", "a");
    	
    	if (fts)
    	{
    		fprintf(fts, "\n%s", randString);
    		fclose(fts);
    	}
    
    
    	else
    	{
    		printf("Error writing to file!\n");
    	}
    
    }
    
    
    char* random(char* randString, int finish)
    {
    	int check = 1;
    
    	if(strcmp(randString,"") == 0)
    	{
    		strcpy(randString, "CHHHH");
    	}
    
    	else(strcmp(randString,"") != 0);
    	{
    		if (strcmp(randString, "CHHHH") == 0)
    		{
    			while(check != 0)
    			{
    				strcpy(randString,"NHHHH");
    				check = 0;
    			}
    		}
    		printf("It infinite loops here");
    
    		finish = 1;
    	}
    		
    
    
    return(randString);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why did you make a new thread for this same topic?

    You don't have a while on 63. You have an else. The semicolon there effectively removes the else, and makes everything after it happen regardless of anything before it.


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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    See the other thread - this one is done.
    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.

Popular pages Recent additions subscribe to a feed