Stuck making part of my code work for testing input

This is a discussion on Stuck making part of my code work for testing input within the C Programming forums, part of the General Programming Boards category; Hi everyone, I am new to the forums but not new to the site. I have this little project for ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Arrow Stuck making part of my code work for testing input

    Hi everyone, I am new to the forums but not new to the site.

    I have this little project for school that we are using our microcontroller boards with.

    The project was to take the code given to us (95% of what you see here) that was an odd and even program, to modify it so that 'e' will turn it on, and 'd' will disable it.

    I got that part working initially, but in the last step we had to check so that only numbers 0-9, 'e', and d are accepted.

    My instructor is known for lots of bad ways of coding, like the while(1) loops, so don't eat me up for those.

    I started with a error function (now commented out), that I replaced the character input with at first, but it was giving me problems so I tried making it work just inside the main function.

    its telling me that the while(character == "1"... is always true as one of the compile errors along with the while loop.

    I look at my code and still think its right. If anything other then d is entered, it should test the odd/even if a numbered is entered, or loop back and ask to enter another number if a false character is given. D should disable it with the else if statement.

    Can you help me please, thanks

    Code:
    /*	Serial Communication exercise on the Axiom CME11E9 EVBU and PC.	        */
    /*	A character is entered on the keyboard in terminal mode and it    		        */
    /*	is returned to the terminal by the EVB over COM1 with a message		        */
    /* 	stating whether it is even or odd and prompting another entry. 		       	        */
    /*									    	        */ 
    /****************************************************************************/
    /*   				Includes (libraries)	      	    		         */
    /****************************************************************************/
    #include <hidef.h>
    /****************************************************************************/
    /*   				  Definitions					         */
    /****************************************************************************/
    #define	baud *(volatile unsigned char *)0x102B		/* baud rate reg. 		*/
    #define	sccr1 *(volatile unsigned char *)0x102C		/* sci cont. reg. 1 		*/
    #define	sccr2 *(volatile unsigned char *)0x102D		/* sci cont. reg. 2	 	*/
    #define	scsr  *(volatile unsigned char *)0x102E		/* sci status reg.  		*/
    #define	scdat *(volatile unsigned char *)0x102F		/* sci data reg.   		*/
    #define tdre 0x80					/* transmit mask bit	*/
    #define rdrf 0x20					/* receive mask bit 	*/
    /*****************************************************************************/
    /*   		         Declarations (variables and function prototypes)		           */
    /*****************************************************************************/	 	
    char input(void);
    void onsci(void);
    /*void error(void);*/
    void output(char);
    void outcrlf(void);  
    void outstr(char *strg);
    char Msg1[] = "Enter a number...";
    char Msg2[] = "Enter another number..";
    char Msg3[] = "... is an odd number";
    char Msg4[] = "... is an even number";
    char Msg5[] = "The Evaluator Is Off, Press 'e' To Enable It";
    char Msg6[] = "The Evaluator Is On, Press 'd' To Disable It";
    char Msg7[] = "You entered an invalid character, please try again!";
    char character;
    /*****************************************************************************/
    /*   					Main				  	           */
    /*****************************************************************************/
    void main(void) 
    {			
     EnableInterrupts;
     onsci();
     outstr(Msg5);
     outcrlf();
     while(1)
      {
       character = input();
       if (character == 'e')
      		{
      		outstr(Msg6);
     		outcrlf();
      		outstr(Msg1);
      		outcrlf();
      
      while(character != 'd')
      	{
       	character = input();
       	output(character);
       	if (character == '0' || '1' || '2' || '3' || '4' || '5'|| '6' || '7' || '8' || '9')
       		{
       		if ((character & 0x01) == 0 )
       		{
       		outstr(Msg4);
       		} 
      	    else
      	    {
      	    outstr(Msg3);
      	    }
      	outcrlf();
      	outstr(Msg2);
      	outcrlf();
      	outcrlf();
       		}
    	}
    	}
    	else if (character = 'd')
    	{
    	main();
    	}
    	
    }   
    }  			   
    
    /****************************************************************************/
    
    /****************************************************************************/
    /*   				Locally Defined Functions			        */
    /****************************************************************************/
    
    /*void error(void)
    {
    
    character = input();
    while(character != '1' || '2' || '3' || '4' || '5'|| '6' || '7' || '8' || '9' || 'e' || 'd') 
    {
    outstr(Msg7);
    outcrlf();
    character = input();
    }
    
    }
    */
    
    /****************************************************************************/
    void outstr(char *strg)				            /*  Output a string of characters   */
    { 						              /*  String ends in NULL             */
    	 char character;				    /*  Exits loop when onechar = NULL  */
         while (character = *strg)
         {
          output(character);
          *strg++;
         }
    }
    
    /****************************************************************************/
    
    char input(void)			   		          /*  Read a character from the sci    */
    {							/*  wait for rx data then get it     */
     while ((scsr & rdrf) == 0 ){};
     return scdat;		 	      		   				
    }
    
    /****************************************************************************/
    
    void onsci(void)			            /*  Initialize SCI for 9600 baud with 8 MHz xtal.    */
    {
     baud  = 0x30;
     sccr1 = 0x00;
     sccr2 = 0x0C;
    }
    
    /****************************************************************************/
    
    void output(char character)		  		            /* output one character  */
    {						       /* wait for tx data reg. empty, then   */ 
     while ((scsr & tdre) == 0){};
     scdat = (character & 0x7F); 	      		      /* clear MSB (parity bit) and send.    */
    }
    
    /****************************************************************************/
    
    void outcrlf(void)  	  		 	 /* output a carriage return and line feed  */
    {			 
     output(0x0D);
     output(0x0A);
    }
    
    /****************************************************************************/
    /*   				   End of module.				        */
    /****************************************************************************/

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > if (character == '0' || '1'
    This should be
    if (character == '0' || character == '1'
    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.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    such a simple mistake, I should have picked up on that.

    Thanks my friend

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    I got it partially working, but I it was kind of messy. So I reorganized the code, but haven't got it working since.

    I've tried in the numcheck function

    Code:
    while(character !='1' || .... character !='9' || character !='d')
    also tried with converting it to decimal using

    Code:
    character = character - 48;
    
    then using 
    
    while((0 > character > 10) || character !='d')

    I then resorted to this, still no luck. The program will recognize the d to disalble the program now, but still gives invalid character for anything else.

    Code:
    /*	Serial Communication exercise on the Axiom CME11E9 EVBU and PC.	        */
    /*	A character is entered on the keyboard in terminal mode and it    		        */
    /*	is returned to the terminal by the EVB over COM1 with a message		        */
    /* 	stating whether it is even or odd and prompting another entry. 		       	        */
    /*									    	        */ 
    /****************************************************************************/
    /*   				Includes (libraries)	      	    		         */
    /****************************************************************************/
    #include <hidef.h>
    /****************************************************************************/
    /*   				  Definitions					         */
    /****************************************************************************/
    #define	baud *(volatile unsigned char *)0x102B		/* baud rate reg. 		*/
    #define	sccr1 *(volatile unsigned char *)0x102C		/* sci cont. reg. 1 		*/
    #define	sccr2 *(volatile unsigned char *)0x102D		/* sci cont. reg. 2	 	*/
    #define	scsr  *(volatile unsigned char *)0x102E		/* sci status reg.  		*/
    #define	scdat *(volatile unsigned char *)0x102F		/* sci data reg.   		*/
    #define tdre 0x80					/* transmit mask bit	*/
    #define rdrf 0x20					/* receive mask bit 	*/
    /*****************************************************************************/
    /*   		         Declarations (variables and function prototypes)		           */
    /*****************************************************************************/	 	
    char input(void);
    void onsci(void);
    void numcheck(void);
    void oncheck(void);
    void output(char);
    void outcrlf(void);  
    void outstr(char *strg);
    char Msg1[] = "Enter a number...";
    char Msg2[] = "Enter another number..";
    char Msg3[] = "... is an odd number";
    char Msg4[] = "... is an even number";
    char Msg5[] = "The Evaluator Is Off, Press 'e' To Enable It";
    char Msg6[] = "The Evaluator Is On, Press 'd' To Disable It";
    char Msg7[] = "You entered an invalid character, please try again!";
    char character;
    /*****************************************************************************/
    /*   					Main				  	           */
    /*****************************************************************************/
    void main(void) 
    {			
     EnableInterrupts;
     onsci();
     outstr(Msg5);
     outcrlf();
     oncheck();
    
    while(1)
      {
      outstr(Msg6);
      outcrlf();
      outstr(Msg1);
      outcrlf();
      
      while(1)
      	{
       	numcheck();	
       	output(character);
       	if (character !='d')
       		{
       			if ((character & 0x01) == 0 )
       			{
       			outstr(Msg4);
       			} 
      	  	    else
      	    	{
      	   		outstr(Msg3);
      	   	    }
      		outcrlf();
      		outstr(Msg2);
      		outcrlf();
      		outcrlf();
       		}
    	else
    	{
    	outcrlf();
    	main();
    	}
    	
    }
    }   
    }  			   
    
    /****************************************************************************/
    
    /****************************************************************************/
    /*   				Locally Defined Functions			        */
    /****************************************************************************/
    
    void oncheck(void)
    {
    
    character = input();
    while(character != 'e') 
    {
    outstr(Msg7);
    outcrlf();
    character = input();
    }
    
    }
    
    
    void numcheck(void)
    {
    
    character = input();
    character = character -48;
    while((0 > character > 9) || character != 'd') 
    {
    outstr(Msg7);
    outcrlf();
    character = input();
    }
    
    }
    
    /****************************************************************************/
    void outstr(char *strg)				            /*  Output a string of characters   */
    { 						              /*  String ends in NULL             */
    	 char character;				    /*  Exits loop when onechar = NULL  */
         while (character = *strg)
         {
          output(character);
          *strg++;
         }
    }
    
    /****************************************************************************/
    
    char input(void)			   		          /*  Read a character from the sci    */
    {							/*  wait for rx data then get it     */
     while ((scsr & rdrf) == 0 ){};
     return scdat;		 	      		   				
    }
    
    /****************************************************************************/
    
    void onsci(void)			            /*  Initialize SCI for 9600 baud with 8 MHz xtal.    */
    {
     baud  = 0x30;
     sccr1 = 0x00;
     sccr2 = 0x0C;
    }
    
    /****************************************************************************/
    
    void output(char character)		  		            /* output one character  */
    {						       /* wait for tx data reg. empty, then   */ 
     while ((scsr & tdre) == 0){};
     scdat = (character & 0x7F); 	      		      /* clear MSB (parity bit) and send.    */
    }
    
    /****************************************************************************/
    
    void outcrlf(void)  	  		 	 /* output a carriage return and line feed  */
    {			 
     output(0x0D);
     output(0x0A);
    }
    
    /****************************************************************************/
    /*   				   End of module.				        */
    /****************************************************************************/

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    0 > whatever > 10 is valid C, but isn't what you want it to be (and it wouldn't be what you wanted it to be even if the signs were the other way around). 0 > whatever is done first, giving either 0 or 1; this result is then compared > 10, which is always false. Just like you needed || to check two conditions above, you need an operator to check two conditions here.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Your code is hard to follow, because of poor indentation.
    SourceForge.net: Indentation - cpwiki

    And your recursive call to main() is a real killer, if you're running this on a embedded chip with a very limited amount of stack space. What's even more odd is that you have two nested while(1) loops.
    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.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    I suggest using switch/case construct for the menu (the code that decides what to do based on input character).
    And, I agree with Salem that calling main() recursively is very bad.

    I suggest making a function called menu() that displays the menu and returns the option selected.
    Note: It is also common to have a function displayMenu() to show the menu called from menu(); but, you code seems to not really be giving the user a menu. But, other than that it is acting like a menu.

    Tim S.
    Last edited by stahta01; 01-25-2011 at 07:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get sample shell code to work...
    By jcafaro10 in forum C Programming
    Replies: 6
    Last Post: 07-07-2010, 08:04 AM
  2. Help with part of code.
    By ajdspud in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2006, 04:21 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  4. Stuck on Input problem
    By Syneris in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2006, 09:53 PM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

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