-
Function calling woes
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
Code:
#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: main.c
Code:
#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: ranget.c
Code:
#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);
}
file: check_error.c
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);
}
There are three other files, they do the work of generating the numbers, writing them to the file..etc.
-
Can't u write three files in one file with three functions???
-
check_error (p); // attempt to call function check_error before any
// random numbers are generated.
// issues c2065 "p" undeclared identifier
Well 'duh', what did you expect?
Forget the name you call the parameter in the function prototype, and pick an integer in scope which you want to error check, say
check_error( n );
But at that point in the code, n is an uninitialised variable.
Also, check_error() is not exactly the best name for the function, given what that function actually does.
> srand(time(NULL)); // issues error c4244 possible loss of data
Try a cast
srand( (unsigned int)time(NULL)); // issues error c4244 possible loss of data
-
I'll be the first to admit, I really stink at programming. I've only been doing it a few weeks and well, it shows. I was hoping to get a bit more guidance. If I didn't feel stupid before, I really do now.
-
And then, after feeling as stupid as a worn, stained rug, I sat. I thought. And it came to me. I solved the problem. Thanks, I think, for making me feel dumb. It brought out the best in me.
-
And don't you forget it!
gg
-
Funny thing, mostly I am pretty stupid, I mean, I have to think alot harder than it seems most common folks have to, in order to reach the same ends, but it sure feels good to see it to the end on my own.