Thread: Menu showing twice?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Menu showing twice?

    I cant seem to see why ... maybe i have been looking at the code too long, but the Welcome and selection part load only once first up, though when a selection is made, it displays it twice ...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
     
    void initialise();
    void listDir();
    void displayBitmap();
    
    struct DirEntry
    {
    	int UserCode;
    	char FileName[9];
    	char FileExt[4];
    	char Extent;
    	int BlockCount;
    	int DiskBlock[16];
    } DirSys[32];
    
    char Bitmap[23];
    char selct;
    
    int main()
    {
    do 
    	{
    	printf(" ------------------\n");
    	printf(" Welcome to \n");
    	printf(" My CP/M Simulator \n");
    	printf(" ------------------\n");
    	printf(" Enter (i)nitialise, (l)ist directory, (d)isplay bitmap or (e)xit: ");
    	scanf("%c", &selct);
    	printf("\n");
    	switch (selct)
                    {
    		case 'i':
    		initialise();
    		break;
                 	
    		case 'l':
    		listDir();     
    		break;
                 	
    		case 'd':
    		displayBitmap();     
    		break;
    		
    		case 'e':
    		exit(0);
    		break;
    		}
    	}while(1);
    		;			
    }
    
    void initialise()
    {
    	int i, j;
    	
    	/*Initialise bitmap */
    	Bitmap[0]=254;
    	for (i=1; i <23; i++)
    		Bitmap[i]=255;
    	
    	/*Initialise Directory */
    		for (j=0; j <32; j++)
    			DirSys[j].UserCode = -1;
    		
    }
    
    
    void listDir()
    {
    	int j, count;
    	printf(" ----------------------------------- \n"); 
    	printf(" File Name | File Type | Block Count \n"); 
    	printf(" ----------------------------------- \n");
    	for (j=0; j <32; j++)
    	{
    		if (DirSys[j].UserCode != -1)
    			printf("%s  %s  %i \n",DirSys[j].FileName, DirSys[j].FileExt, DirSys[j].BlockCount);
    		else
    			(count++);	
    	}
    
    	if (count == 32);
    		printf(" ERROR: Empty Directory Structure \n");
    
    }
    
    void displayBitmap()
    {
    	int i,lb=0;
    	unsigned char m;
    	for(i=0;i<23;i++)
    	{
    		m=1;
    		while(m)
    		{
    			if(Bitmap[i]&m)
    			{	
    				printf(" ");
    				putchar('1');
    			}	
    			else
    			{
    				printf(" ");
    				putchar('0');
    			}
    			m<<=1;
    			lb++;
    			if(lb%20==0)
    				putchar('\n');
    		}
    	}
    	printf("\n\n");
    	
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Thumbs up

    use
    Code:
    gets(&selct);
    
    insted of 
    
    scanf("%c", &selct);
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by maven
    use
    Code:
    gets(&selct);
    
    insted of 
    
    scanf("%c", &selct);
    NEVER USE GETS

    Code:
    Never use gets().  Because it is impossible to tell without knowing the data in advance how many characters gets()
           will  read,  and because gets() will continue to store characters past the end of the buffer, it is extremely dan-
           gerous to use.  It has been used to break computer security.  Use fgets() instead.
    
           It is not advisable to mix calls to input functions from the stdio library with low-level calls to read() for  the
           file  descriptor  associated  with  the input stream; the results will be undefined and very probably not what you
           want.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Even if it were a good idea to use gets, which it isn't, their use of it would be wrong. The s in gets is for string. A single character is not a string. It's a character. Now, you can have a string of one character, but that string contains only the null character. You cannot have a string of one character, which isn't a null character, because a string by definition is zero or more characters terminated by a null character.

    Thus, you can't take any input as a string, into a single character.

    In any case, the problem is you're reading a single character, but your input is more than one character. You're forgetting about when you are pressing enter after you type a character. Blah blah read the FAQ.


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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > use
    > Code:
    > gets(&selct);
    Do everyone a favour and remain silent until you've learnt some stuff.

    gets() is simply horrid, yet your use is a buffer overflow even by entering just ONE character - that's a new all-time low record - congrats to you
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Menu not showing
    By IdunnO in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2006, 02:29 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM