Checking user input

This is a discussion on Checking user input within the C Programming forums, part of the General Programming Boards category; Hi , Im having some trouble to get this program to work. The user is supposed to enter an integer ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    Checking user input

    Hi , Im having some trouble to get this program to work.
    The user is supposed to enter an integer & then a filename.
    The program should check whether the integer is just that , only an integer & doesn't contain any characters.

    Thanks for any help


    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    void get_info(char *filename,int *n_ptr);
    FILE *cfopen(char *filename,char *mode);
    FILE *gfopen(char *filename,char *mode);
    void prn_rand(FILE *ofp, int n);
    int check(int *n_ptr);
    
    FILE *fp;
    int digit,flag,i,n;
    
    void main(){
      char file[25];
      FILE   *ofp;
      get_info(file,&n);
      ofp = cfopen(file,"w");
      
      for(i = 1;i <= n;++i){
    	  fprintf(ofp,"%12d", rand());
    	  if (i % 6 == 0 || i == n)
    		  fprintf(ofp,"\n");
      }
      prn_rand(ofp, n);
    }
    void get_info(char *filename,int *n_ptr){
    	do{
    	printf("\n%s\n\n%s",
    		"This program creates a file of random numbers.",
    		"How many random numbers would you like? ");
    	     if(fgets((char)n_ptr,sizeof(n_ptr),stdin) != NULL);
                  digit = check(n_ptr);
    
    	     if (digit == 1)
    		      printf("ERROR:type in an integer again.");
    	} while (digit == 1);
    		
    	printf("\nIn what file would you like them? ");
    	scanf("%s",filename);
    }
    FILE *cfopen(char *filename,char *mode){
    	char reply[2];
    	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){
       if ((fp = fopen(filename,mode)) == NULL) {
    	   fprintf(stderr, "Cannot open %s -bye!\n",filename);
    	   exit(1);
       }
       return fp;
    }
    
    void prn_rand(FILE *ofp, int n)
    {
       printf("\nHello from prn_rand()\n\n");
    }
    
    int check(int *n_ptr){ 
        i = 0;
    	flag = 0;
    	while((i < sizeof(n_ptr)) && (flag == 0)){
    		if (isalpha(n_ptr[i]))
    	      flag = 1;
    	    ++i;
    	}
    	if (flag == 0)
    		return 0;
    	else
    		return 1;
    }
    Im guessing the problem is either somewhere with useing (or not useing the address operator) , & the pointers.??
    As I have some trouble understanding these concepts ...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I haven't read the whole thing yet, but this is definitely wrong:
    Code:
    	     if(fgets((char)n_ptr,sizeof(n_ptr),stdin) != NULL);
    What you have to do is something like:
    Code:
    fgets(some_string_array, sizeof(some_string_array), stdin);
    *n_ptr = atoi(some_string_array);
    EDIT: Reading a bit further, you want to call your check() function on the some_string_array. Then your check() function could look like this:
    Code:
    int check(char *str)
    {
      while(*str)
      {
        if(!isdigit(*str))
          return 1;
        str++;
      }
    
      return 0;
    }
    Last edited by itsme86; 08-05-2004 at 08:00 AM.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks for the help
    I've changed my code a bit , but im still getting debug errors.



    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    void get_info(char *filename,char *integer);
    FILE *cfopen(char *filename,char *mode);
    FILE *gfopen(char *filename,char *mode);
    void prn_rand(FILE *ofp, int n);
    int check(char *integer);
    
    FILE *fp;
    int digit,flag,i,n;
    
    void main(){
      char file[25];
      char integer[3];
      FILE   *ofp;
      get_info(file,&integer);
      ofp = cfopen(file,"w");
      
      for(i = 1;i <= n;++i){
    	  fprintf(ofp,"%12d", rand());
    	  if (i % 6 == 0 || i == n)
    		  fprintf(ofp,"\n");
      }
      prn_rand(ofp, n);
    }
    void get_info(char *filename,char *integer){
    	do{
    	printf("\n%s\n\n%s",
    		"This program creates a file of random numbers.",
    		"How many random numbers would you like? ");
    	     if(fgets(integer,sizeof(integer),stdin) != NULL);
                  digit = check(integer);
    	     if (digit == 1)
    		      printf("ERROR:type in an integer again.");
    	} while (digit == 1);
    		
    	printf("\nIn what file would you like them? ");
    	scanf("%s",filename);
    }
    FILE *cfopen(char *filename,char *mode){
    	char reply[2];
    	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){
       if ((fp = fopen(filename,mode)) == NULL) {
    	   fprintf(stderr, "Cannot open %s -bye!\n",filename);
    	   exit(1);
       }
       return fp;
    }
    
    void prn_rand(FILE *ofp, int n)
    {
       printf("\nHello from prn_rand()\n\n");
    }
    
    int check(char *integer){ 
        i = 0;
    	flag = 0;
    	while((i < strlen(integer)) && (flag == 0)){
    		if (isalpha((int)integer[i]))
    	      flag = 1;
    	    ++i;
    	}
    	if (flag == 0)
    		return 0;
    	else
    		return 1;
    }

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    what errors are you getting?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Your get_info() function is still not correct. Stuff that's wrong:

    - You're passing &integer to the get_info() from main(). You should just be passing integer.
    - The second arg to fgets() in in your get_info() is sizeof(integer). It should be 3. If you do sizeof() on a pointer you'll only get the size of the pointer (generally 4 bytes). This is a common mistake people make.

    I haven't looked at the rest yet, but you should fix those couple of things and go from there.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    cool , it works now ! thanks alot itsme it was very nice of you to help me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  4. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  5. checking user input for "type"
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 12-24-2001, 04:25 AM

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