Thread: how to specify the input field.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    how to specify the input field.

    Hye all. Im new to this forum. I would like to seek some help here.
    Below is my source code for counting the net salaray of a worker. My question is, how for example, in the name area, I only want the user to input character, other than that, invalid data and for id only numbers ? And, how at the end of the program, to make the user input only y for continue the program, and n to exit the program.?

    Code:
    #include <stdio.h>
    
    void main()
    {	
    	
    	int id;																				
    	char month[100],position[500],name[500],ex;											
    	double basic_salary,EPF_deduction,gross_salary,total_OT_payment,net_salary,OT_rate; 
    	double OT;																			
    
    	OT_rate = 50; 
    	ex = 'y';
    
    	
    		do	
    	
    	{
    		printf("\n\nPlease enter month (Capital Letter): ");
    		scanf("%s",month);
    
    		
    		printf("\n\n*******************************************************************");
    		printf("\nPAYMENT SLIPS FOR THE MONTH %s 2005\nSMART CHOICE EDUCATION CENTRE\n",month);
    		printf("*******************************************************************\n");
    			
    		
    		printf("Staff name\t\t: ");
    		scanf("%s",&name);
    		printf("Staff ID\t\t: ");
    		scanf("%d",&id);
    		printf("Position\t\t: ");
    		scanf("%s",&position);
    
    		printf("*******************************************************************\n");
    		
    		
    		printf("\nBasic Salary\t\t: RM ");
    		scanf("%lf",&basic_salary);
    		
    		
    		EPF_deduction = basic_salary * 0.1;
    
    		printf("EPF Deduction\t\t: RM %.2lf\n",EPF_deduction);
    		
    		
    		gross_salary = basic_salary - EPF_deduction;
    
    		printf("Gross Salary\t\t: RM %.2lf\n",gross_salary);
    		
    		
    		
    
    		printf("*******************************************************************\n");
    		
    		
    		printf("\nTotal OT hour\t\t: ");
    		scanf("%lf",&OT);
    
    		printf("OT Rate \t\t: RM %.2f\n",OT_rate);
    
    		
    		total_OT_payment = OT * OT_rate;
    
    		printf("Total OT Payment \t: RM %.2lf\n",total_OT_payment);
    		
    
    		printf("*******************************************************************\n");
    
    		
    		net_salary = gross_salary + total_OT_payment;
    
    		printf("Net Salary\t\t: RM %.2lf",net_salary);
    		
    
    		printf("\n*******************************************************************\n");
    												
    		
    		printf("Thank you , for using this program and have a nice day.\n");
    		
    		
    		printf("Do you want to continue?(y/n) : ");
    		scanf("%s",&a);
    
    
    	}
    		
    		while (ex =='y' || ex == 'Y');
    }
    I'll be glad to here from ya'll. Thank you

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    You can use getch it will still have them enter whatever they want. It will only display the character if they enter a y or n. It is pretty complicated though.
    Code:
    char response;
    printf("Continue? (y/n) :");
    while(response != 'y' && response != 'n')
    {
         response = getch();
    }
    putchar(response):
    getchar();
    if(response == 'y')
    {
         /*Code Here*/
    }
    else if(response  == n')
    {
         /*Code Here*/
    }
    getch gets a character without printing it onto the screen. The user does not need to hit enter either.
    Here is the breakdown:
    1. The Continue question is asked.
    2. It assigns the character that the user enters to response without printing it onto the screen. When that character is either a y or n it breaks out of that loop.
    3. Putchar puts the y or n out onto the screen.
    4. Getchar waits for them to press enter to continue.
    5. The if and else statements would contain your code for what to do.

    This gives the appearance of just having it only get the character if they enter a y or n. I think that this is what you were looking for. Hope this helps.

    Note: getch is not included on all compilers. If you state your compiler it can help. If using Dev-C++ you include <conio.h>
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by 00Sven
    You can use getch it will still have them enter whatever they want. It will only display the character if they enter a y or n. It is pretty complicated though.
    Code:
    char response;
    printf("Continue? (y/n) :");
    while(response != 'y' && response != 'n')
    {
         response = getch();
    }
    putchar(response):
    getchar();
    if(response == 'y')
    {
         /*Code Here*/
    }
    else if(response  == n')
    {
         /*Code Here*/
    }
    getch gets a character without printing it onto the screen. The user does not need to hit enter either.
    Here is the breakdown:
    1. The Continue question is asked.
    2. It assigns the character that the user enters to response without printing it onto the screen. When that character is either a y or n it breaks out of that loop.
    3. Putchar puts the y or n out onto the screen.
    4. Getchar waits for them to press enter to continue.
    5. The if and else statements would contain your code for what to do.

    This gives the appearance of just having it only get the character if they enter a y or n. I think that this is what you were looking for. Hope this helps.

    Note: getch is not included on all compilers. If you state your compiler it can help. If using Dev-C++ you include <conio.h>
    ~Sven
    The code is broken. The prototype for getchar() is
    int getchar(void);

    This means that the variable response should be declared as an int and not a char. This comes down to the fact char may be either signed or unsigned. If the machine is signed, getchar() will return -1 on error. However, if the machine it unsigned, the machine will return 255. If you mucky on this, I recommend getting a basic book on computer science that talks about hex to dec conversions.

    Regards
    Last edited by cdalten; 05-01-2006 at 07:07 PM.

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I do not refer to the return value of getchar. I simply use it to pause the program. You could take this out of the program but then as soon as they hit y or n it would do the code for that choice without them needing to hit enter. If you want it can be replaced with...
    Code:
    while( ( temp = getchar() ) != '\n' && temp != EOF );
    This would keep on doing getch and placing the character into a temp which would need to be an int and then once the user hit enter it would continue with the program. This way the only thing that is output is the y or n and then nothing else. But it still waits until Enter is pressed.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    And not testing for EOF leads to undefined behavior.

    Code:
    #include <stdio.h>
    
    int main(void) {
        char response;
        printf("Continue? (y/n) :");
        while(response != 'y' && response != 'n')
        {
            response = getchar();
        }
        putchar(response);
            getchar();
        if(response == 'y')
        {
            /*Code Here*/
        }
        else if(response  == 'n')
    {
         /*Code Here*/
    }
    
    
    }
    $./asc
    Continue? (y/n) :z

    <-----here the program hangs.
    using your revised code
    Code:
    while( ( temp = getchar() ) != '\n' && temp != EOF )
    while having response declared as int would be a much sounder solution. With that, I need to cook some dinner.

  6. #6
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I thought that I had corrected that but I must have forgotten to. You are right response should be an int not a char.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And don't use void main(). Use int main().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM