Thread: struct question part 2

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

    struct question part 2

    Before I go on with this program, I'd like to know if what I've done thus far is ok in terms of valid code. Please, if you see something wrong, tell me so that I can fix it.

    TIA... formatting doesn't look so good in preview mode... hmm

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h> 
    
    typedef struct data1
    {
      char questions[25][75];	
      char iname[20];
      char ipassw[15]; 
      int num2ask;	
    	
    } progD;
    
    void newD(progD *temp)
    {
        /* 
         * newD()
         *  
         * purpose: to store initial information provided
         *          by the instructor, pw, and the set of
         *          questions to be asked.
         *          
         * status:  Incomplete - struct "data1," output file,
         *                       #defines, pw, entering
         *                       questions as a sep funct
         ********************************************************/
         
        char *n, num[5];
        int start=0;
            
        printf("Please enter your name: ");
        fgets(temp->iname, 20, stdin);    
        
        /* any nasty side effects from doing this? */
        temp->iname[strlen(temp->iname)-1] = '\0';    
        
        printf("\n%s, how many questions will you ask?  (MAX = %d): ", temp->iname, 25);
        fgets(num, sizeof(num), stdin);
        
        num[strlen(num)-1] = '\0';
        
        /* if num is not in the desired range, then output such to correct */ 
        /* working as expected? not all is as it seems.... */
        
        while((temp->num2ask = (int)strtol(num, &n, 10)) > 25 || temp->num2ask < 1)
        {
           	/* some conditions still sneak through :( */
           	 
           	printf("\nSorry %s, %d is not a valid value.\n", temp->iname, temp->num2ask);
           	
           	printf("Please enter a value between 1 and 25: ");
           	fgets(num, sizeof(num), stdin);
        }    
        
        while(start < temp->num2ask) /* get those questions */
        {
        	/* replace test with local var later on */
        	/* real mode dos target, so question length is 80 - "-> " to avoid line wrap */
    
        	printf("\nYou have %d question(s) remaining\n", (temp->num2ask < 25 ? temp->num2ask-start : 25-start));
        	printf("-> ");                                      /* above will be modifed at some point */
        	
        	fgets(temp->questions[start], 75, stdin);    	
        	temp->questions[start][strlen(temp->questions[start])-1] = '\0';
        	
        	start++;
        }    
    }	
    
    int main(void)
    {
      int i;
    	
      progD user;	
      newD(&user);
    	
      i=0;
    	
      printf("\n\n%s, you asked the following\n\n", user.iname);
    	
      while(i < user.num2ask)
        printf("%s\n", user.questions[i++]);		
    	
      return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by ronin
    Before I go on with this program, I'd like to know if what I've done thus far is ok in terms of valid code. Please, if you see something wrong, tell me so that I can fix it.

    TIA... formatting doesn't look so good in preview mode... hmm

    Didn't find anything glaringly wrong, althought I didn't look extremely close. I did notice one thing though:

    Code:
         
        char *n, num[5];
        int start=0;
            
        printf("Please enter your name: ");
        fgets(temp->iname, 20, stdin);    
        
        /* any nasty side effects from doing this? */
        temp->iname[strlen(temp->iname)-1] = '\0';    
    /* only if they type in 20 or more characters -- the 20th character will be overwritten.
       Might want to consider increasing the input buffer for safety's sake
        
        printf("\n%s, how many questions will you ask?  (MAX = %d): ", temp->iname, 25);
        fgets(num, sizeof(num), stdin);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    >> num[strlen(num)-1] = '\0';
    >This FAQ shows you how to safely remove the newline from an fgets() buffer.

    whats wrong with what he had?

    Code:
    /* this just add a extra function. */
    
    /* shouldnt it be strrchr anyway? */
    if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Hey WaltP.. thanks for the feedback.

    Thanks Salem!!.... your suggestions were exactly what I was hoping for when I decided to free my favorite mod to torment over this stage in the program .

    This is a pure dos program, but isn't there some sort of interupt handler to squash ctrl-z? I'd like to implement one later on so that user's can't kill the app that way.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Doh.... I meant ctrl-c

    Thanks for the replies Salem... they're appreciated.
    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. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. struct question
    By ctovb in forum C Programming
    Replies: 3
    Last Post: 08-13-2008, 11:23 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM