Thread: prompting to enter a name in a loop skips the name enter part

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    prompting to enter a name in a loop skips the name enter part

    Code:
    	/* Prompt user for the employees name, hours worked, and hourly rate */
    	for (x = 0; x < num_emp; x++)
    	{
    		printf ("Enter the name for employee #%i: ", x + 1);
    		gets (emp_name);
    		printf ("Enter the number of hours employee #%i worked: ", x + 1);
    		scanf ("%i", &hours_worked);
    		printf ("Enter the hourly rate of employee #%i: ", x + 1);
    		scanf ("%i", &hourly_rate);
    	}
    basically in the ouput it skips the enter name for employee n part and goes right to enter the number of hours employee n worked.

    i know i am absolutely not suppose to use the gets() function so i think that might be part of my problem. but i need to enter a full name (like John Smith) so i dont think i can use scanf because it will stop at the first space.

    so what can i do to modify my code so it will correctly prompt for the entering of the name and will hold both the first and last name.

    when i used scanf to enter the whole name, if i put just the first name it worked fine. if i entered a whole name (with a space) the entire program would jumble up and skip to the last employees info being entered from that array.

    help is awesome!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    gets() is a problem, but you would get the same problem if you switched to fgets() (though you should switch). The problem is that the second scanf() leaves the newline in the buffer, and this newline is immediately snapped up by the gets() (or fgets()) call on the next iteration of the loop. You need to read and ignore what is left on the buffer until the newline at the end of each iteration.
    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

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    so should i use fflush (stdin); at the end of the loop?

    and i am still very new to this, but how is fgets() used?

    thank you

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kisiellll
    so should i use fflush (stdin); at the end of the loop?
    No, as that results in undefined behaviour since fflush() is only supposed to work on output streams. Rather, read the FAQ: How do I get my program to wait for a keypress? The myflush() function can be adapted or even just copied for use in this case.
    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

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    so the only way for the program to not skip over the user name entering part is to use the functions in that FAQ you posted?

    i seem to believe there is an easier way to do this. i am in an intro to c course and my professor has not gone over anything with making functions like that and even suggested the use of gets().

    is there anyway i could just use gets() in that loop? i am only asking because i do not want to over complicate my code for what is suppose to be a simple program in an intro to c course.

    there are other methods, and i absolutely know yours would work. its just that this professor likes things done by his guidelines so i am trying to stick to them as much possible with this program.

    thanks again.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    o if it makes any difference emp_name is actually an array emp_name[30][10]

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kisiellll
    so the only way for the program to not skip over the user name entering part is to use the functions in that FAQ you posted?
    You do not actually have to use any of those functions since you just need to do what myflush() does.

    Quote Originally Posted by kisiellll
    o if it makes any difference emp_name is actually an array emp_name[30][10]
    No, as I stated, the problem is that the newline is left in the buffer. As long as you do not remove it, you next call, whether it be to fgets() or gets(), will not read in what you want to read in.
    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
    Feb 2009
    Posts
    37
    ok i got things to work in a method that is what my professor has sort of guidelined for us but theres one problem with the ouput.

    heres the code now:

    Code:
    /* Prompt user for the employees name, hours worked, and hourly rate */
    	for (x = 0; x < num_emp; x++)
    	{
    		fflush(stdin);
    		printf ("Enter the name for employee #%i: ", x + 1);
    		gets (emp_name[x]);
    		printf ("Enter the number of hours employee #%i worked: ", x + 1);
    		scanf ("%i", &hours_worked);
    		printf ("Enter the hourly rate of employee #%i: ", x + 1);
    		scanf ("%i", &hourly_rate);
    		
    		/* Determine the gross pay */
    		if (hours_worked > 40)
    		{
    			
    	}
    	
    	/* Display the results for each of the employees entered */
    	printf ("Payroll Report\n");
    	printf ("--------------\n\n");
    	for (x = 0; x < num_emp; x++)
    	{
    		printf ("%s\n", emp_name[x]);
    	}
    	
    	return 0;
    
    } /* end main */
    right now im just making sure its outputting the names correctly put there coming out like this:

    John SmithJane Doe
    Jane Doe

    why would this be happening?

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    never mind the gross pay part, didnt mean to paste that in there

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If your "professor" is teaching you junk like fflush(stdin) and gets(), then you need another professor.
    Be aware that he's teaching you how to walk, by first breaking your legs.
    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. Einstine calculator Mark 2
    By Mach_ie in forum C Programming
    Replies: 1
    Last Post: 08-31-2003, 01:54 PM
  2. Caught in a Loop
    By aprilbiz in forum C Programming
    Replies: 16
    Last Post: 07-22-2002, 06:06 PM
  3. I need help badly
    By taz_jiggy in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 09:36 PM
  4. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 01:23 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM