my code.. remember i am a rookie dont laugh

This is a discussion on my code.. remember i am a rookie dont laugh within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> struct delta { char name [30] ; int age; int smoke_non ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Smile my code.. remember i am a rookie dont laugh

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    struct delta {
    	char name [30] ;
    	int age;
    	int smoke_non   ;
    	char gender [1] ;
    	int  seat_number ;
    	float price;
    	float discount_pass;
    	char  student_id [1] ;
    	int i ;
    	int k ;
    	};
    int main(void)
    { //int main open
    	int i=0;
    	int k=0;
    	int length;
    	char  charvar;
    				
    struct delta del_seat[5];
    	do
    	{ //do open
    		for( int i=0; i<5 ; i++)
    		{ //for i open
    		printf( " Welcome to Delta.. We get you there, if the program runs properly.\n");
    		printf( " Please enter name...\n");
    		scanf("%s",&del_seat[i].name);
    		length= strlen(del_seat[i].name);
    
    for( int k=0; k<length ; k++)
    {   //for k open
    				
                          if (del_seat[i].name[k] =='!' ||
    					
    
                         del_seat[i].name[k] =='@' ||
    					
                        del_seat[i].name[k] =='#' ||
    					
                       del_seat[i].name[k] == '$' ||
    					
                      del_seat[i].name[k] == '%' ||
    					
                      del_seat[i].name[k] == '^' ||
    					
                     del_seat[i].name[k] ==  '&' ||
    					
                    del_seat[i].name[k] ==  '*' ||
    					
                   del_seat[i].name[k] ==  '(' ||
    					
                   del_seat[i].name[k] ==  ')' )
    
    {
    					
    printf("Entry Error..This is not a proper name.\n");
    	break;
    }
    }
    			
    if (isdigit(del_seat[i].name[0])|| isdigit(del_seat[i].name[length-1]))
    {
    					
    printf("Not a valid entry..This is not a proper name.\n");
    	break;
    			}
    		
    			 //age check//
    					
    printf( "Please enter age of customer.\n");
    					
    scanf("%d" , &del_seat[i].age);
    			  
    while       ((del_seat[i].age  >=100) && (del_seat[i].age  <=0))
     {
    					
    printf( " This is not a valid age...enter age again.\n");
    					
    del_seat[i].age=0 ;
    					
    scanf( "%d" , &del_seat[i].age);
      }
    		/// smoking prefrence///
    
    					
    printf( " If customer a Smoker-->(1)....Non-Smoker-->(2).\n");
    					
    scanf("%d" , &del_seat[i].smoke_non);
    				
    while(( del_seat[i].smoke_non <1)||(del_seat[i].smoke_non >2))
    {
    					
    printf( " This is not a valid smoking prefrence...try again.\n");
    					
    scanf( "%d" ,del_seat[i].smoke_non );
    break;
    
    }
    
    
    		///gender   My problem is here    ///
    
    					
    printf( " If customer a MALE-->(m)....FEMALE-->(f).\n");
    					
    scanf( "%d" , del_seat[i].gender);
    					
     del_seat[i].gender = getchar();
    				
    while( del_seat[i].gender !='m' && del_seat[i].gender !='f')
    {
    					
    printf( " This is not a valid entry...try again.\n");					
    scanf( "%" ,&del_seat[i].gender);
    					
    del_seat[i].gender = getchar();
    					}
    		///seat#///
    
    
    		/// discount pass///
    
    
    		///student id///
    
    		} ///this is near the end
    
    
    	}
    
    	while (i<5);
    
    
    	return 0;
    }


    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > char gender [1] ;
    Anytime you find yourself using an array of size one, just use a single variable of that type. Change this to

    char gender;

    and see how that works for you.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Re: my code.. remember i am a rookie dont laugh

    Originally posted by aubrey
    Code:
    
    		///gender   My problem is here    ///
    
    					
    printf( " If customer a MALE-->(m)....FEMALE-->(f).\n");
    					
    scanf( "%d" , del_seat[i].gender);
    					
     del_seat[i].gender = getchar();
    				
    while( del_seat[i].gender !='m' && del_seat[i].gender !='f')
    {
    					
    printf( " This is not a valid entry...try again.\n");					
    scanf( "%" ,&del_seat[i].gender);
    					
    del_seat[i].gender = getchar();
    					}


    Dunno if you've fixed this or not, and it's likely not the way the professional programmers here would handle it, but I rather like

    Code:
    int main(void)
    {
        
        char newline;
        struct delta del_seat[5];
             
    	
    	for(;;)
    	{
    		printf( "\nEnter (m) for male, (f) for female, or (d) for done: ");					
    	
    		// get rid of this scanf( "%d", &del_seat[i].gender); 
    	
    		scanf("%c%c", &del_seat[0].gender, &newline); // try this 
    	
    		// I'd stay away from this too.. but that's just me ...  /del_seat[i].gender = getchar();	 
    		
    		del_seat[0].gender = tolower(del_seat[0].gender);
    	
    		if(del_seat[0].gender == 'd') break;	
    
    		switch(del_seat[0].gender)
    		{
    			case 'm': printf("male selected.\n");
    		    	      break;
    		
    			case 'f': printf("female selected.\n");
    		    	      break;
    		
    			default: printf("Invalid selection: %c\n", del_seat[0].gender);
    		    	     break;
    		}; 
    	}
    		 
    
    	return 0;
    
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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