Function calling woes

This is a discussion on Function calling woes within the C Programming forums, part of the General Programming Boards category; I am starting out with two seperate programs, the first one is one that asks the user how many random ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    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.
    Semper Fi!

  2. #2
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    Can't u write three files in one file with three functions???

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    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.
    Semper Fi!

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    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.
    Semper Fi!

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    And don't you forget it!

    gg

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    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.
    Semper Fi!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM

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