Thread: function calling within another function error

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    function calling within another function error

    Hi guys im always running into problems when im passing variables into functions and so forth.

    Im getting some sort of cast pointer from integer problem im trying to call
    the validateMonth function inside getMonth()..

    the call to getUserInput() works perfectly but i wont call the validateMonth i get an error does anyone know what im doing wrong?

    I need the parameters to have unsigned month so i can use the variable in the validateMonth() function unsigned validateMonth(unsigned month)

    Code:
    unsigned getMonth()
    {
       
       /*** declare variables*/
      
      unsigned  i;
      int  valid = 0;
      char *prompt = "Please enter a month between 0 - 12\n";
      char *month;
      
      do
      {
      month = getUserInput(prompt);
      month = validateMonth(i);
      }
      while(!valid);
    
      return EXIT_SUCCESS;
    the other function:

    Code:
    unsigned validateMonth(unsigned month)
    {
         unsigned m;
         while(month<0 || month>12)
         {
            
            printf("Month error 0 or less or equal to 12 please\n");
            
         }
    
         return m;
    }

    these are my prototypes in my .h

    Code:
    unsigned getMonth();
    unsigned getYear();
    void displayCalendar(unsigned month, unsigned year);
    char* getUserInput(char *prompt);
    unsigned validateMonth(unsigned month);
    void readRestOfLine();

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    month = getUserInput(prompt);
    month = validateMonth(i);
    Why are you assigning the return value of getUserInput to month, and then writing over top of it with validateMonth, without doing anything with it? You're passing i to validateMonth. Shouldn't you be passing it month?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In addition
    Code:
      do
      {
      month = getUserInput(prompt);
      month = validateMonth(i);
      }
      while(!valid);
    i is uninitialised, so you pass garbage to validateMonth
    valid is never modified, so it loops forever.

    This is your 6th thread on your calendar program and you're still making very basic mistakes. Are you actually learning anything or just making random edits until someone posts you the solution?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM