Thread: homework help - arrays (I think)

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

    homework help - arrays (I think)

    Hello,

    This is my first post here. I just started a C programming class and I'm a little stuck. My assignment is to evaluate an input code (example TRV2475A5R-14) and determine if:
    * it is the proper length (the example is the full length)
    * the first string of four numbers are all numbers (and no letters got stuck in there)
    * if the final letter (the R before -14) is R and the four digit number is over 2000, then it's banned.
    * it should also keep track of the number of valid codes and banned codes.
    * last, the user should signal end of file to stop the program.

    I already know I've done some things wrong, but first off I'm not sure what the correct approach is. We just started learning about arrays so I'm not comfortable with them. My main questions are:

    * How do I determine that the code is too short? I've been trying to input all the characters in a character array, and I'm not sure that's really the correct approach. Should I have multiple arrays for the characters / integers?
    * Similar to above, how do I stop it at EOF? I don't think it works if it's asking for array inputs (in my current code). Can I enter an if condition (if %c == EOF) to break? I'm not even that sure how break works... we've only used it in one switch statement. That continue loop is just temporary, by the way. I know it's wrong.

    Here's my code... any insight would be greatly appreciated. I'm not afraid of following up on my own if you can just point me in the right direction. Please bear in mind that I'm only a few weeks into this class.

    Thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    char prod_code[12]; char cont='y';
    int i, banned_code=0, valid_code=0;
    
    printf( "\nWelcome to the Product Code validator.\n" );
    	printf( "Please enter your Product Code. It should consist of numbers (N) and letters (L) in the folowing format: \n" );
    	printf( "   LLLNNNNLNL-NN\n" );
    
    while( (cont=='y')||(cont=='Y') )
    	{
    	printf( "Enter your code here: " );
    
    	for(i=0; i<12; i++ )
    		printf( "%c", prod_code[i] );
    
    	if( ((prod_code[9]=='R')||(prod_code[9]=='r')) && (prod_code[3]>='2') )
    		{
    		printf( "Banned code\n" );
    		banned_code ++;
    		}
    	else
    		{
    		printf( "Valid code\n" );
    		valid_code ++;
    		}
    		printf( "Do you want to continue? (y/n): " );
    		fflush ( stdin );
    		scanf( "%c", &cont );
    	}
    	
    printf( "You entered %d banned code(s) and %d valid codes. ", banned_code, valid_code );
    
    printf( "End of Program\n" );
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would take in the whole product code in a string, first. THEN lay out your series of simple blocks of code to make your tests.

    Code:
    #include <string.h>
    
    and you can use something like this, which is not meant to be runnable code, 
    but idea code to springboard your own thoughts.
    
    set banned equal 0
    
    if(strlen(pcode) less than 13) {
      printf("Invalid - product code is too short\n");
      banned++;
      //maybe return
    }
    
    /* find the string of first 4 digits, and confirm there are 4 and they are adjacent */
    //first, move past any initial letters in the code
    
    i=0;
    //move past initial letters or other chars
    while(pcode[i] greater than '9' || pcode[i] less than '0') 
      ++i;
    
    //test for 4 digits in a row. Watch for an off by one error here
    for(j=0;j<4;j++) {
      if(pcode[i] < '0' || pcode[i] > '9')
        break; //exit the for loop
      ++i;
    }
    if(j < 4) {
      printf("Invalid product code. Four consecutive digits are not present in the first group\n");
      banned++;
    }
    It's best to keep it to one test at a time. Making those tests on a string just seems like a natural way to do it.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thank you so much for your help! We haven't covered strings yet, but just from reading your explanation I think I get the idea. I will look ahead in my textbook as well as at the tutorials on the site. I'm not sure if strings were meant to be covered in my class by now and we're just behind (which is making this assignment so tricky) or if there is supposed to be an answer that doesn't use strings.

    I will post back once I have a solution in hand or if I get hopelessly stuck.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thank you for your help so far, and I think I have it mostly working. My only problem is getting it to end... can someone help me make the program stop when I just hit enter? Using a condition for pcode[0] != EOF or '\n' doesn't seem to do anything... those probably can't be entered into an array anyway, right? Any help would be appreciated... I can never seem to get EOF to work for me.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char pcode[20];
    	int banned=0, valid=0, invalid=0;
    
    	printf( "\nWelcome to the Product Code validator.\n" );
    		printf( "Please enter your Product Code. It should consist of numbers (N) and letters (L) in the folowing format: \n" );
    		printf( "   LLLNNNNLNL-NN\n" );
    
    	while( pcode[0] != EOF )
    		{
    		printf( "\nEnter your code here: " );
    	
    		scanf( "%s", &pcode );
    		
    		printf( "%s\n", &pcode );
    		
    		if( strlen (pcode) < 13) 									/* check if input is too short*/
    		{
    			printf( "Invalid - product code is too short\n" );
    			invalid++;
    		}
    		
    		else if( strlen (pcode) > 13) 								/* check if input is too long*/
    		{
    			printf( "Invalid - product code is too long\n" );
    			invalid++;
    		}
    		else if( ( (pcode[3]) < '0' || (pcode[3]) > '9' ) || ((pcode[4]) < '0' || (pcode[4]) > '9' ) || ((pcode[5]) < '0' || (pcode[5]) > '9' ) || ((pcode[6]) < '0' || (pcode[6]) > '9' ) || ((pcode[7]) < '0' || (pcode[7]) > '9' ) )
    		{
    			printf( "Invalid - Region is non-numeric\n" );
    			invalid++;
    		}
    		else if( ((pcode[9]=='R')||(pcode[9]=='r')) && (pcode[3]>='2') )	/* check if input is from banned region*/
    		{
    		printf( "Banned code\n" );
    		banned++;
    		}
    		else
    		{
    		printf( "Valid code\n" );
    		valid++;
    		}
    			
    		}
    		
    	printf( "You entered %d banned code(s), %d invalid codes, and %d valid codes. ", banned, invalid, valid );
    
    	printf( "End of Program\n" );
    
    	return 0;
    
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your while loop, needs to basically be a an endless loop:

    Code:
    while(1) {
    
       /* after this line, add this to end your looping */
       scanf( "%s", &pcode );
       if(pcode[0] == '\n') //no entry, just the enter key was hit
          break;   //so break out of this loop.
    Give that a try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertex Arrays
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 01-08-2006, 01:24 AM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM