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
Im guessing the problem is either somewhere with useing (or not useing the address operator) , & the pointers.??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; }
As I have some trouble understanding these concepts ...



LinkBack URL
About LinkBacks



