Thread: Filling an array

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    Yes. Data printed to the screen is not guaranteed to be displayed if it doesn't end with a newline (I think), so one should use fflush() in that case. Files are different.
    Yes. By default, stdout/stdin are line buffered, unless they have been redirected to a file. (The standard actually says that it's line buffered if it is connected to an "active device", a.k.a. a terminal.) All other FILE objects are fully buffered by default. So if you are fprintf()'ing to a file, even a newline won't necessarily cause a flush.

    Always flush when you print any sort of prompt. And it's usually a good idea to flush whenever you are printing debug or log messages. If your program crashes, whatever data is in the buffer won't get flushed. That data might contain critical information that could help you solve the crash, so you should ensure that it actually gets flushed.

  2. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Here is what I have no besides your size variable lol.

    Code:
    void get_Pay_Rate (int id[], float pay_Rate[], int hour[])
    {
    	
    
    	int i = 0;
    	int emp_Count = 0;
    	float pay_Rates = 0.0;
    	
    	printf("Please enter pay rates that match the following Id's! \n\n");
    	for (i = 0; i < MAX_ARRAY;)
    	{
    			printf( "ID: %d\tPayrate: \tHours: ", id[i] );
    			fflush( stdout );
    			scanf_s("%f", &pay_Rate[i]);
    			scanf_s("%d", &hour[i]);
    				if (pay_Rate[i] < 6.0) 
    				{
    					printf("You have entered an invalid payrate; please try again!\n\n");
    					continue;
    				}
    				else if (hour[i] < 0)
    				{
    					printf("You have entered an invalid amount of hours; please try again!\n\n");
    					continue;
    				}
    				else 
    				{
    				i++;
    				}		
    	}
    
    }  // End of get_payRate
    what do I need to do to make all the inputs line up nice? The hours input lines up nicely but the other doesn't.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Umm . . . print some extra spaces?
    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.

  4. #19
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Lining things up really only works if the data isn't being entered dynamically or if you're only entering one thing at the end of the line like with the id and payrate. You have to hit enter to send input to the program, so something like this might be better:
    Code:
    printf( "\nID: %d\nPayrate: ", id[i] );
    fflush( stdout );
    scanf_s("%f", &pay_Rate[i]);
    printf( "Hours: " );
    fflush( stdout );
    scanf_s("%d", &hour[i]);
    That puts everything nicely on a single line and groups them together. It's not as concise, but it works for any number of fields.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Filling a 2d Array cause program to crash
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2003, 07:00 AM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 03:11 PM