Thread: gets function skipped 1st time

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    2

    gets function skipped 1st time

    I have searched for a solution and understand that dislike of the "gets" command; however, I have to do this for a homework assignment.

    My issue is that I have to write a program that ask users for name of employee as well as hourly rate and hours worked, then calculate using functions a number of items. I have all of this done except that I can't figure out the user input for FIRST employee.

    I had a solution using scanf (" %[^\n]", emp_name[e]); that worked but is not allowed. I fixed what I originally did by adding a getchar() to end of my for loop and that will give me the separate lines for employee name and hourly rate for the 2nd - 10th employee.

    So I just don't know what do for the 1st employee - I tried adding fflush statements before, adding space after scanf statement for # of employees.

    It will work for the other employees - it just skips that first employee.


    Enter the number of salaries to post (1-10): 2


    Enter the name of employee 1: Enter the number of hours for : 10
    Enter the hourly rate for : 10


    Enter the name of employee 2: Debbie
    Enter the number of hours for Debbie: 10
    Enter the hourly rate for Debbie: 10


    I attached the portion of my file that is regards to this problem, but here's the code as well.

    Code:
    
    
    Code:
    do
        {    
            printf("Enter the number of salaries to post (1-10): ");
            scanf("%i" , &numberEmployees);
            fflush(stdin);
        
            if( numberEmployees < 1 )
                printf("ERROR! - must post at least 1 salary!\n\n");    
            if( numberEmployees > 10 )
                printf("ERROR! - cannot post more than max (10)!\n\n");    
         
        }     while( numberEmployees < 1 || numberEmployees > 10 );    
        
        printf ("\n");
    
    
        for (e = 0; e < numberEmployees; ++e)
        {
        
        printf("Enter the name of employee %i: ",e+1);
        gets(emp_name[e]);
            
        /* Input for hours must be greater than 0 */    
        do    
        {    
        
            
            printf("Enter the number of hours for %s: ", emp_name[e]);
            scanf("%f", &hours_worked);
            fflush(stdin);        
            
            if ( hours_worked < 1 )
                printf ("ERROR! - number of hours must be greater than 0\n"); 
    
    
        }    while ( hours_worked < 1 );
    
    
        /* Input for hourly rate must be greater than 0 */
        
        do
        {         
            printf("Enter the hourly rate for %s: ", emp_name[e]);
            scanf("%f", &hourly_rate);
            fflush(stdin);
            
            if( hourly_rate < 1 )
                printf ("ERROR! - hourly rate must be greater than 0\n"); 
            
        }     while( hourly_rate < 1 ); 
    
    
    getchar();
        
    }


    Thanks for any suggestions!
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    The best answer is to use fgets() to read a whole line into a temporary buffer for ALL input.
    Then use sscanf / strcpy / whatever to parse that buffer and copy the relevant data into your array.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The concerns with gets() and fflush(stdin) specifically aside .... it is not a good idea to mix line oriented input (fgets(), gets() [if you use it despite being advised not to], etc) with formatted input from the same stream.

    The reason is that scanf() - with most format specifiers - stops when it encounters a newline, and leaves the newline in the stream. Line oriented input respond to any stray newlines remaining in the stream (and act as if a line with no characters has been entered). This is the underlying reason for the problem here .... and is the reason an approach like Salem describes is usually recommended.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    2
    We haven't covered fgets() so that can't be used either - I've read quite a bit in the programming boards as to why gets is wrong as well as fflush(stdin). We have to use fflush() after every scanf statement and gets is only way that we "know" how to enter a string of characters for this assignment/class. Any other ideas within this context using the above code?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ambrown782
    We have to use fflush() after every scanf statement and gets is only way that we "know" how to enter a string of characters for this assignment/class.
    If fflush(stdin) works for you in other situations, it should work here, so I don't understand the problem. You could try using gets once before the for loop to read and discard whatever is left on the line.

    By the way, which school are you from and what class are you taking? Do you happen to have the email address of your teacher? If so, send me a private message with these details as I would like to have a word with your teacher. (Unfortunately, you can only send private messages after you have been active for awhile with several posts.)
    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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ambrown782 View Post
    We haven't covered fgets() so that can't be used either.
    Since when are students limited to using only what the teacher has taught, or prevented from reading ahead in a textbook? All you need to do is refer to appropriate documentation about fgets() and justify your claim that it meets the needs.

    If you're afraid to do that, it is possible (with due care due to specifics of its behaviour) to use scanf() in place of gets() to read a string. It's more work than using fgets() but certainly possible, and not particularly difficult.

    Quote Originally Posted by ambrown782 View Post
    We have to use fflush() after every scanf statement
    Why? You've been told it is invalid, and the link Salem gave explaining that also provides a link to an alternative technique. There is nothing preventing you using things the teacher hasn't taught, as long as you justify your case, check quality of your source information (e.g. by crosschecking it with information in your textbook), and cite them.

    You need to be careful in how you justify what you do, whether in class or in a workplace. A teacher won't accept a statement of "The information you gave in class is crap, so I'm doing this instead". A decent teacher will accept "Our textbook on page 47 describes this, which is better for this purpose, and standard".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > We have to use fflush() after every scanf statement and gets is only way that we "know" how to enter a string of characters for this assignment/class
    Seriously, you might be better off just dropping the class and finding someone who has a clue to teach you.

    Your current "tutor's" approach is basically one of "I'm going to teach you to run, but first I'm going to break your legs".

    You've got to wonder how much more brain-rot they're going to give 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: 01-12-2013, 10:11 AM
  2. gets() skipped!
    By ncode in forum C Programming
    Replies: 14
    Last Post: 02-06-2012, 05:01 PM
  3. Scanf Being skipped
    By GibsMe in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 03:23 PM
  4. scanf is being skipped
    By yougene in forum C Programming
    Replies: 6
    Last Post: 12-24-2008, 06:05 AM
  5. Second scanf keeps getting skipped.
    By yougene in forum C Programming
    Replies: 9
    Last Post: 12-23-2008, 02:39 AM