#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Learning C - Critique My Code


    Hey just looking for a critique on the 'properness' of my code, this program works as intended but just lookin for tips where i could improve layout/flow for where im at now. Thanks in advance

    Code:
    //Learn to make shapes based on user input on size using loops, nested loops, switches and anything you can throw in that you have learned so far
    #include <stdio.h>
     
    
    int main(){
    	int selection;
    	
    	whichObject(selection);
    	
    	//Asks user if they would like to run the program again
    	printf("\nWould you like to run the program again? \n");
    	printf("1 - Yes\n");
    	printf("2 - No\n");
    	printf("Enter Selection: ");
    	scanf("%d", &selection);
    	printf("\n");
    	
    	switch(selection){
    		case 1:
    			main();
    			break;
    		case 2:
    			printf("Thanks for trying me out. See Ya!\n");
    			break;
    	}
    	return 0;
    }
    
    //Asks the user which object they would like to make
    int whichObject(int selection){
    	printf("What object would you like to create?\n");
    	printf("1 - Triangle\n");
    	printf("2 - Right Triangle\n");
    	printf("3 - Upside Down Triangle\n");
    	printf("4 - Sideways Triangle\n");
    	printf("Enter Selection: ");
    	scanf("%d", &selection);
    	printf("\n");
    	
    	switch(selection){
    		case 1:
    			triangle();
    			break;
    		case 2:	
    			rightTriangle();
    			break;
    		case 3:
    			upsideDownTriangle();
    			break;
    		case 4:
    			sidewaysTriangle();
    			break;
    	}
    	
    	return 0;
    }
    
    //Makes a sideways triangle based on the size inputed 
    int sidewaysTriangle(){
    	int size,line,i;
    	
    	printf("How big would you like your triangle?\n");
    	printf("(Note even #'s do not work here): ");
    	scanf("%d", &size);
    	printf("\n");
    
    	//top half of triangle
    	for(line=1;line<=size/2;line++){
    		for(i=1;i<=line;i++){
    			printf("*");
    		}
    		printf("\n");
    	}
    	
    	//Middle
    	for(i=1;i<=(size+1)/2;i++){
    		printf("*");
    	}
    	printf("\n");
    	
    	//bottom half of triangle
    	for(line=size/2;line>=0;line--){
    		for(i=1;i<=line;i++){
    			printf("*");
    		}
    		printf("\n");
    	}
    	
    }
    
    //Makes an upside down triangle based on the size inputed by user
    int upsideDownTriangle(){
    	int size,line,i;
    	
    	printf("How big would you like your triangle? ");
    	scanf("%d", &size);
    	printf("\n");
    	
    	//creates the upside down triangle
    	for(line=1;line<=size;line++){
    		//prints space
    		for(i=1;i<=line;i++){
    			printf(" ");
    		}
    		
    		//prints *'s
    		for(i=1;i<=size-line;i++){
    			printf("*");
    		}
    		printf("*");
    		for(i=1;i<=size-line;i++){
    			printf("*");
    		}
    		
    		//prints space
    		for(i=1;i<=line;i++){
    			printf(" ");
    		}
    		printf("\n");
    	}
    }
    
    
    //Makes a triangle based on the size inputed by user
     int triangle(){
    	int size,line,i;
    	
    	printf("How big would you like your triangle? ");
    	scanf("%d", &size);
    	printf("\n");
    	
    	//creates the triangle
    	for(line=1;line<=size;line++){
    		//prints the spaces
    		for(i=1;i<size-line+2;i++){
    			printf(" ");
    		}
    		
    		//prints the *'s
    		for(i=1;i<=line-1;i++){
    		printf("*");
    		}
    		printf("*");
    		for(i=1;i<=line-1;i++){
    			printf("*");
    		}
    		
    		//prints the spaces
    		for(i=size-line;i>=0;i--){
    			printf(" ");
    		}
    		printf("\n");
    	}
    	return 0;
    }
    
    //Makes a right triangle based on the size inputed by user
    int rightTriangle(){
        int size,line,i;
    	
    	printf("How big would you like your right triangle? ");
    	scanf("%d", &size);
    	printf("\n");
    	
    	//Creates the right triangle
    	for(line=1;line<=size;line++) {
    		for(i=1;i<=line;i++){
    			printf("*");
    		}
    		printf("\n");
    	}
    	
    	return 0;
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,580
    Rep Power
    1906
    You should not call main() inside main().
    It will be better to create a loop, which will end when the user select "no".

    It is also a good practice to add an option for "default" value.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Ya after lookin that over i should of directly called whichObject() right there instead of main(). And if i use a while statement would I really need the case statements or would this work better?


    Code:
    int main(){
    	int selection;
    	
    	while(selection!=2){
    		whichObject(selection);
    	
    		//Asks user if they would like to run the program again
    		printf("\nWould you like to run the program again? \n");
    		printf("1 - Yes\n");
    		printf("2 - No\n");
    		printf("Enter Selection: ");
    		scanf("%d", &selection);
    		printf("\n");
    	}
    	
    	return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo