Thread: Please help on my Payroll system project

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    8

    Question Please help on my Payroll system project

    hi, im having trouble with my project. its a payroll system. i dont know how the name i inputted will be printed correctly. when i use scanf, it just print the first set of words. i learned that i should use strlen() but when i try to use it, it never let me to input then exit the program. what should i do? here's the code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    #define p printf
    #define s scanf
    #define g gotoxy
    
    main()
    {
    
    int select;
    char name[30];
    int n;
    
    clrscr();
    
    p("[1] 15th \n[2] Monthly \n\nSelect mode of payment: ");
    s("%d", &select);
    
    if(select==1)
    {
    clrscr();
    p("15th \n\n");
    p("Enter employee's name: ");
    gets(name);
    n=strlen(name);
    p("Employee's Name: %s", name);
    }
    
    else if(select==2)
    {
    clrscr();
    p("Monthly \n\n");
    }
    
    else
    {
    clrscr();
    p("Invalid Selection");
    }
    
    getch();
    }
    i need this tomorrow morning so please help me, thank you

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fflush vs gets
    Very common question.
    Don't forget that gets is not safe function. EDIT:http://c-faq.com/stdio/getsvsfgets.html
    And what are these #define for??? I won't give any mark if I were to give mark.
    Last edited by Bayint Naung; 08-09-2010 at 11:18 AM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    Quote Originally Posted by Bayint Naung View Post
    fflush vs gets
    Very common question.
    Don't forget that gets is not safe function.
    And what are these #define for??? I won't give any mark if I were to give mark.
    define? its for those printf, scanf. so that i will just type p for printf and so on. so, i will not use gets? oh man...how can i do this

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Ew, macro abuse. How hard it is to type out printf? Yeesh.

    scanf leaves the newline in the input buffer. You need to flush out the input buffer with something like

    Code:
    int p;
    while ( (p=getchar())!='\n' && p!=EOF);
    Alternatively, use scanf or fgets (never gets) for everything to avoid the conflicts.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    Quote Originally Posted by KBriggs View Post
    Ew, macro abuse. How hard it is to type out printf? Yeesh.

    scanf leaves the newline in the input buffer. You need to flush out the input buffer with something like

    Code:
    int p;
    while ( (p=getchar())!='\n' && p!=EOF);
    Alternatively, use scanf or fgets (never gets) for everything to avoid the conflicts.
    so where should i put this? and what does it do?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quick! Get me my shotgun, darling!

    Got us some pesky defines hanging 'round, and I sure don't want to miss 'em.

    Cute code, ain't - remember that.

    Put it where the sun --- oh no, put it wherever you need to help clear the newline from the keyboard buffer.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marchyde07
    so where should i put this? and what does it do?
    You put it where you "need to flush out the input buffer", and it discards input until a newline or EOF condition, hence "flushing" the input buffer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    If you read in something and it leaves crap in the input buffer, and you need to flush out the input buffer, then where, logically, would you put the code to do the flushing? ^_^

    And I'm not saying another word until those defines have been nuked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  4. Another question on classes
    By hpy_gilmore8 in forum C++ Programming
    Replies: 26
    Last Post: 05-24-2003, 09:11 AM
  5. DJGPP project problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 07:16 PM