I am starting out with two seperate programs, the first one is one that asks the user how many random numbers they want, and then it writes these to a file. The program worked fine (notice past tense) until I tried to call the second program. It will still work great if I comment out the function call to error_check.
The second program is one that Thantos helped me with, what it does is looks at the users input and will only accept positive integers. What I am trying to do now is to meld these two together, so that when the user is asked how many random numbers, the code will loop back to that question until a valid integer is entered. My first attempt was to call the "error_check" function first, and if it was a valid input, store it in an array, and then return it to the main program. When I did that, it messed up the srand function (issues error that I am taking a number and puttin it into a smaller one) and it also tells me that the function error_check has an undeclared identifier. The next attempt was to call the error_check function in the same function that gets the users input. That fixed the problem with srand, but I still get the undeclared identifier error. I would like some help figuring out how to declare this identifier so I can get on with the show.
Thanks!
Here is the code I think is culpritting me...(is that even a word?)
file: careful.h
file: main.cCode:#include <stdio.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #include <string.h> #define MAXLINE 100 #define MAXSTRING 100 typedef const char cchr; FILE *cfopen(cchr *filename, cchr *mode); FILE *gfopen(cchr *filename, cchr *mode); void check_error(int p); void get_info(char *filename, int *n_ptr); void prn_rand(FILE *ofp, int n);
file: ranget.cCode:#include "careful.h" int main(void) { char ofn[MAXSTRING]; // outfile name int n; FILE *ofp; srand(time(NULL)); // issues error c4244 possible loss of data check_error (p); // attempt to call function check_error before any // random numbers are generated. // issues c2065 "p" undeclared identifier get_info(ofn, &n); ofp = cfopen(ofn, "w"); prn_rand(ofp, n); return 0; }
file: check_error.cCode:#include "careful.h" void get_info(char *filename, int *n_ptr) { printf("\n%s\n\n%s", "This program creates a file of random numbers.", "How many random numbers would you like? "); scanf("%d", n_ptr); // check_error(p); will issue Undeclared Identifier p, error printf("\nIn what file would you like them? "); scanf("%s", filename); }
There are three other files, they do the work of generating the numbers, writing them to the file..etc.Code:#include "careful.h" void check_error(int p) { char line[MAXLINE]; int i; int length; int error = 0; do { error=0; printf(" How many random numbers would you like? \n"); printf("\n"); fgets(line, MAXLINE, stdin); sscanf(line, "%d", &p); length = strlen(line); if ( line[length-1] == '\n' ) { --length; line[length]='\0'; } for (i = 0; i < length; ++i) { if(!isdigit(line[i])) { error = 1; break; } } if (error) { printf("\n Try again!"); } else { printf("\n Looks like an integer to me. Thanks!\n"); } } while (error); }



LinkBack URL
About LinkBacks


