Thread: Filling an array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    All it need liked you said was the _getch().
    _getch() isn't portable, you should be using getchar() instead. It doesn't do exactly the same thing, but it's close enough not to matter.
    Also is this "fflush( stdout );" just clearing the buffer?
    It makes sure that the message is really printed before scanf() starts waiting for input. If it isn't, the user will sit around wondering what to do.

  2. #2
    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. #3
    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