Thread: Testing user input question?

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Hoser83
    This may be a dumb question, but what does the BUFSIZ mean. It that a default thing?
    No, BUFSIZ is a user-defined constant. He wrote the code that way to tell you, the one taking the example, it's variable to whatever you want it to be.
    Sent from my iPadŽ

  2. #17
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    so base 10 equals a decimal integer?

  3. #18
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes. Do you understand the concept of bases, though? It's a basic computer concept that will be very essential in your progamming future.
    Sent from my iPadŽ

  4. #19
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Here is my entire code:

    I'm not able to use my compiler right now so I'm just trying to get close:

    My book has not gone over bases yet. That is why I'm sort of lost here.

    If this does work, how do I use what is in the string to get the correct number of random numbers printed. Right now, I use a for loop, but I don't know for sure how to use the for loop with the string.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLINE   100
    
    FILE *cfopen(char *filename, char *mode);
    
    FILE *gfopen(char *filename, char *mode);
    
    int main(void)
    {
         FILE  *ofp;
         int     i, line[MAXLINE];
         char  filename[120];
         char  *pEnd;
         long   1;     
    
        prinf("\n%S\n\n%s",
    	"This progam creates a file of random numbers.",
    	"Please enter in the number of random numbers
                      you would like to see as a positive integer.   "),
        fget( line, MAXLINE, stdin);
        
        if((l = strtol(line, &pEnd, 10)) <= 0 || *pEnd != '\n')
             {
                printf("\nERROR: Please enter positive integer.\n");
             }
        
         printf("\nIn what file would you like them?  ");
         scanf("%s", filename);
    
         ofp = cfopen(filename, "r+");
    
          
            
            for(i=1; i<=line; ++i){
               fprintf(ofp, "%12d", rand());
               if(i%6 == 0 || i == n)
                  fprintf(ofp, "\n");
         }
    }
    
    
    
    
    
    FILE *cfopen(char *filename, char *mode)
    {
        char  reply[2];
        FILE  *fp;
     
        if(strcmp(mode, "w") == 0
          && (fp = fopen(filename, "r")) != NULL){
            fclose(fp);
    	printf("\nFile exists.  Overwrite it? ");
            scanf("%1s", reply);
            if(*reply != 'y' && *reply != 'Y') {
            printf("\nBye!\n\n");
    	exit(1);
    	}
          fp = gfopen(filename, mode);
          return fp;
    }
    
    
    
    FILE *gfopen(char *filename, char *mode)
    {
        FILE *fp;
        
        if((fp = fopen(filename,mode)) == NULL){
        fprintf(stderr, "Cannot open %s - bye!\n", filename);   
        exit(1);
        }
        return fp;
    }
    Last edited by Hoser83; 02-15-2006 at 03:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  2. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  3. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 08:57 PM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  5. Beginner question- user input termination
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 02:48 PM