Hey guys im having allot of trouble im using a rather modulized solution which is how i need it for my assignment.

However when i enter input using a function called getUserInput() that i created it accepts input correctly, but when it validates the input using my validateMonth() function it dosnt validate properly.

Could someone take a look at it. Iv attached the file in a zip it needs to be compiled on a unix ansi c compiler thnxs

If someone could solve it and explain to me why this is occuring it would be great

Recommended Answers

All 16 Replies

Member Avatar for iamthwee

Why not post it here instead of attaching it.

Insert coda here

Not many will bother downloading an attachment you little newbie.

/****************************************************************************
* COSC1097/1098/1283/1284 - Programming Principles 2A/Programming Techniques
* Semester 1 2006 Assignment #1 - cal program
* Full Name        : EDIT HERE
* Student Number   : EDIT HERE
* Yallara Username : EDIT HERE
* Course Code      : EDIT HERE
* Program Code     : EDIT HERE
* Start up code provided by Steven Burrows & Daryl D'Souza
****************************************************************************/

#include "cal.h"

/****************************************************************************
* Function main() is the entry point for the program.
****************************************************************************/
int main(void)
{
   unsigned  month;
   unsigned  year;
   
  
   
   month = getMonth();
   year  = getYear();
   return EXIT_SUCCESS;
}


/****************************************************************************
* Function getMonth() prompt the user for a number MIN_MONTH to MAX_MONTH and
* returns that number. The number 0 is valid because this indicates that the
* user wants to select all months.
****************************************************************************/
unsigned getMonth()
{
   
   /*** declare variables*/
  
  unsigned  tmpMonth;
  int  valid = 0;
  char *prompt = "Please enter a month between 0 - 12 !\n";
  char *month;
  unsigned valMonth;
  
  
  /*do
  {*/
  
  month = getUserInput(prompt);
  month = validateMonth(prompt,month );
  /*}
  while(!valid);*/
  /*printf("hello %s",month);*/
  /*printf("Please enter a month between 0 - 12\n");*/

 
  /* validation check if month entered is less then 0 > 12*/ 
 
  
  /*do
  {
        fgets(buff, BUFF_SIZE, stdin);
	
        
	*month = atoi(buff); 
       
	while(flag == 1 || *month <0 || *month >12)
	{
	   flag = 0;
	   
	   printf("Wrong input for month from 0 - 12 only!\n");
	   printf("Please enter a month between 0 - 12\n");
	   
	   fgets(buff, BUFF_SIZE, stdin);
	   *month = atoi(buff); month = getUserInput(prompt, result);
  tmpMonth = validateMonth(valMonth, prompt, result);
	
	}
   
	month
       
	
	                    
	
       
   }
   while(flag != 0 );
          */
        
  
   
   return EXIT_SUCCESS;

}


/****************************************************************************
* Function getYear() prompts the user for a number MIN_YEAR to MAX_YEAR and
* returns that number.
****************************************************************************/
unsigned getYear(char *month)
{
   char *prompt = "Please enter the year\n";
   char *year;
   
   year = getUserInput(prompt);
   
   
   return EXIT_SUCCESS;
}


/****************************************************************************
* Function displayCalendar() displays the calendar for the user.
* The function will display the calendar for a whole month.
* If the user supplied a month of "0", then a calendar for a whole year
* is displayed instead w char *result;ith each month displayed under the previous one (you
* don't need to try to display months side by side).
* Give attention to getting the output format exactly as shown below
* (including headings and alignment). Here's an example for March 2006:
* --------------------
*      March 2006
*  S  M Tu  W Th  F  S
*           1  2  3  4
*  5  6  7  8  9 10 11
* 12 13 14 15 16 17 18
* 19 20 21 22 23 24 25
* 26 27 28 29 30 31
* -----validateMonth(month);---------------
***********Kill you*****************************************************************/
void displayCalendar(unsigned month, unsigned year)
{
}


/****************************************************************************
* Function readRestOfLine() is used for buffer clearing. Source: 
* [url]https://inside.cs.rmit.edu.au/~sdb/teaching/C-Prog/CourseDocuments/[/url]
* FrequentlyAskedQuestions/
****************************************************************************/
void readRestOfLine()
{
   int c;

   /* Read until the end of the line or end-of-file. */   
   while ((c = fgetc(stdin)) != '\n' && c != EOF);

   /* Clear the error and end-of-file flags. */
   clearerr(stdin);
}

char* getUserInput(char *prompt)
{
   char *result;
   char buff[BUFF_SIZE];
   
   printf(prompt);
   
   result = fgets(buff, BUFF_SIZE, stdin);
   
   
    if(result == NULL)
    {
       
        printf("Error please enter the input again!\n");
   
    }
    else if(result[strlen(result)-1] != '\n')
    {
        readRestOfLine();
    }
    
  
    return result;
   
   
}

char* validateMonth(unsigned month, char* prompt)
{
     /*unsigned m = 0;*/
     int valid = 0;
     char *m;
     /*int flag = FALSE; */ /* false value*/
   
     if(month<0 || month>12) /* flag 1 for true*/
     {
	
        printf("Month error 0 or less or equal to 12 please\n");
	getUserInput(prompt);
        valid =1;
     }
   
     
     return m;
}
/****************************************************************************
* COSC1097/1098/1283/1284 - Programming Principles 2A/Programming Techniques
* Semester 1 2006 Assignment #1 - cal program
* Full Name        : EDIT HERE
* Student Number   : EDIT HERE
* Yallara Username : EDIT HERE
* Course Code      : EDIT HERE
* Program Code     : EDIT HERE
* Start up code provided by Steven Burrows & Daryl D'Souza
****************************************************************************/

/* Header files. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/* Constants. */
#define MIN_MONTH 0
#define MAX_MONTH 12
#define MIN_YEAR 0
#define MAX_YEAR 9999
#define BUFF_SIZE 80
#define FALSE 0
#define TRUE  1
/* Function prototypes. */
unsigned getMonth();
unsigned getYear();
void displayCalendar(unsigned month, unsigned year);
char* getUserInput(char *prompt);
unsigned validateMonth(unsigned month, char* prompt);
void readRestOfLine();
tmpMonth = validateMonth(valMonth, prompt, result);

What is this parameter result that you are passing? It is not in the declaration or the definition of the function.

Also the

return m;

line in

validateMonth

returns null as you are not assigning any value to m.

Hey wolfPack thnks for replying result is just a variable that is in the input fucntion i created. But im pretty sure its because of how im calling the functions that the month input isnt validating possibly... what do u think? How can it be fixed?

Tell me what is expected as output of this validateMonth function. What is expected for correct input, and what is expected for incorrect input?

char* validateMonth(unsigned month, char* prompt)
{
     /*unsigned m = 0;*/
     int valid = 0;
     char *m;
     /*int flag = FALSE; */ /* false value*/
   
     if(month<0 || month>12) /* flag 1 for true*/
     {
	
        printf("Month error 0 or less or equal to 12 please\n");
	getUserInput(prompt);
        valid =1;
     }
   
     
     return m;
}

I find it strange that you dont get a compiler error at this line.

tmpMonth = validateMonth(valMonth, prompt, result);

Have you defined two

validateMonth

functions?

warning passing argument of 1 integer from pointer without a cast i get that

well the correct input is if i type between 0 - 12 it exits successfully and continues with the rest of the program.

if they type anything less then 0 or more then 12 its an error

Change your function to this and tell me what happens.

int validateMonth(unsigned int month)
{
     if(month<0 || month>12) /* flag 1 for true*/
     {
              return 0;
     }
     return 1;
}

if they type anything less then 0 or more then 12 its an error

Are you sure about that? What month is zero? ;)

i still get the passing arg 1 of validate month makes integer from pointer without a cast

assignment makes integer from pointer without a cast

all on line 51 whichis

month = validateMonth(month /*prompt*/)

i still get the passing arg 1 of validate month makes integer from pointer without a cast

assignment makes integer from pointer without a cast

all on line 51 whichis

month = validateMonth(month /*prompt*/)

That is because month is a character pointer and you are returning an integer from the validateMonth function.
Change that line to

valid = validateMonth(month /*prompt*/);

You should pay attention to all the places you call validateMonth.
Also think about what Bench said.

but if i do that would it still pass what iv inputted in the getUserInput() function into the validateMonth function properly so it can be tested in the condition <0 || >12

ok one error gone iv still got another type cast error at line 51 passing of arg 1 makes integer from pointer without a cast

valid = validateMonth(month) it dosnt like the month variable in the parameters i think

but im still concerned about how i can pass the variable from

month = getUserInput(prompt) so i type in number but then pass it to
valid = validateMonth(month) so that it can validate whatever i input in the first function

sorry its fustrating ey

Ok that compiles but now that it compiles when i run it i type in a number between 0 - 12 its fine which is correct. However if i type -0 or 13 or 14 for example which is <0 || >12 there is no error and there should be saying that its an invalid month try again sorta thing

unsigned getMonth(unsigned month)
{
   
   /*** declare variables*/
  
  unsigned  tmpMonth;
  int  valid = 0;
  char *prompt = "Please enter a month between 0 - 12 !\n";
  char *monthchar;
  unsigned valMonth;
  
  
  /*do
  {*/
  
  monthchar = getUserInput(prompt);
  valid = validateMonth(/*prompt,*/month );

I just saw that these were some startup code that someone else provided. I guess you should figure the rest on your own.

Hi guys im making a minesweeper game and im trying to implement a function that finds the certain blank spaces next to the mines so i can add the number.


i have to Use numbers from 1-8 to mark other squares that have that number of mines adjacent to
them. Squares that don't have adjacent mines are marked as BLANK.


---------------------------
* 1 2 3 4 5 6
* +---+---+---+---+---+---+ [ EXAMPLE 1]
* a | M | M | 1 | | | |
* +---+---+---+---+---+---+
* b | 2 | 2 | 1 | | | |
* +---+---+---+---+---+---+
* c | | | 1 | 1 | 1 | |
* +---+---+---+---+---+---+
* d | | | 2 | M | 2 | |
* +---+---+---+---+---+---+
* e | | | 3 | M | 4 | 1 |
* +---+---+---+---+---+---+
* f | | | 2 | M | M | 1 |
* +---+---+---+---+---+---+
* ---------------------------

#define BLANK ""
#define  MINE 'M'

void placeMines(char minefield[MAX_GRID][MAX_GRID], unsigned size)
{

  double result;
  double sizetotal;
  double roundUp;
  int seed;
  int range;
  int NoMines;
  int TotalMine;
  int i=0;
  int j=0;
  int a=0;
  int b=0;
  
  int count;
  
  char mineChar = MINE;
  
  srand(time(NULL));
  /* square root the grid number*/
  
  sizetotal = size * size; 
  
  /* result is the sizetotal * 0.6*/
  result = (float)(float)sizetotal * MINE_DENSITY;
  
  /*rouding the number up 5.7 up to 6*/
  roundUp = ceil(result);
  
  /* cast it to an int to get no decimal places*/
  printf("NO MINES:%d\n",(int) roundUp);
  
  
  
  
  
  for(i=0; i<roundUp; i++)
  {
     int row, col;
     row = rand() % size;
     col = rand() % size;
     printf("%c %d %d\n",mineChar, row, col);
     
     minefield [row][col]= MINE;
     
     
     printf("\n");
  }
  
	for(i=0; i<size; i++)
  {
     for(j=0; j<size; j++)
     {
     
        
	if(minefield[i][j]==BLANK)
	{
	   if(j==0)
	   {
	      
	  
	     
	        for(a=0; a<size; a++)
		{
	          
		    if(minefield[i][j]==MINE)
	            {
		    
	             count++;
		 
	            }
		 }
		 
		  
		}/* end of outter loop*/
	      
	      
	   }
	   
	   
	
	
	
	     
     }
      
     
	
	
     
  
  }     
	      
	   
	   
	   
	
	
	
	     
     
  
  printGridMineSize(minefield, size);

}

If you have a new question, start a new thread. Since you seem to have solved your old problem, I'm locking this thread. Let me know if I've done so in error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.