Thread: Filling an array

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Filling an array

    Hi I am trying to fill this array with 7 values that correspond to a certain id number. This is what I come up with so far, but it appears to be crashing the program. Not sure what I'm doing wrong. I want the array to hold the pay rates and then print them out.

    Code:
    //Doug Miller
    
    
    #include <stdio.h>
    
    #include <conio.h>
    
    #include "util.h"
    
    #define MAX_ARRAY 7
    
    int id[MAX_ARRAY] = 
    {1111, 2222, 3333, 4444, 5555, 6666, 7777};
    
    float pay_Rate[MAX_ARRAY] = {0.0};
    
    int hour[MAX_ARRAY] = {0};
    
    float salary[MAX_ARRAY] = {0.0}; 
     
    void get_Pay_Rate (int id[], float pay_Rate[]);
    
    int main()
    
    {
    
    int i = 0;
    
    get_Pay_Rate(id, pay_Rate);
    
    for (i = 0; i < MAX_ARRAY; i++)
    {
    printf("%.2f", pay_Rate[i]);
    }
    
    }//END OF MAIN
    
    
    void get_Pay_Rate (int id[], float pay_Rate[])
    {
    	
    
    	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; i++)
    	{
    			for (i = 0; i < MAX_ARRAY; i++)
    			{
    			printf("\n\nPlease enter the pay rate that matches this Id, %d\n\n", id[i]);
    			scanf_s("%f", &pay_Rates);
    				if (pay_Rates < 6.0) 
    				{
    					printf("You have entered an invalid payrate; please try again!\n\n");
    					break;
    				}
    				else 
    				{
    				pay_Rate[i] = pay_Rates;
    				}
    		}
    	}
    
    }  // End of get_payRate

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    	for (i = 0; i < MAX_ARRAY; i++)
    	{
    			for (i = 0; i < MAX_ARRAY; i++)
    Don't reuse loop variables in nested loops, or you'll regret it.

    What is scanf_s()? How does it differ from regular old scanf()?

    BTW, I think perhaps that there is something wrong with your logic here:
    Code:
    			for (i = 0; i < MAX_ARRAY; i++)
    			{
    			printf("\n\nPlease enter the pay rate that matches this Id, &#37;d\n\n", id[i]);
    			scanf_s("%f", &pay_Rates);
    				if (pay_Rates < 6.0) 
    				{
    					printf("You have entered an invalid payrate; please try again!\n\n");
    					break;
    				}
    				else 
    				{
    				pay_Rate[i] = pay_Rates;
    				}
    		}
    Maybe, instead of the for loop, you want something like
    Code:
    do {
        /* ... */
    } while(pay_Rates < 6.0);
    [edit] You don't use conio.h, so you don't have to include it. If util.h needs conio.h then it should include it itself. [/edit]
    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.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    hmm I'm not sure if you understand what I'm trying to do. Before I enter a pay rate I need it to correspond to the id number before hand. This means I need to have the print line include the right id each time. My program works with entering the data. It's displaying the id's and then asking for the rate. However I'm not sure why it's not saying the array and then printing it.

    When I modified the code to this

    I get an endless loop...

    and visual studio says that scanf_s is just a safer function than scanf says I should use it... lol dunno

    Code:
    //Doug Miller
    
    #include <stdio.h>
    
    #include <conio.h>
    
    #define MAX_ARRAY 7
    
    int id[MAX_ARRAY] = 
    {1111, 2222, 3333, 4444, 5555, 6666, 7777};
    
    float pay_Rate[MAX_ARRAY] = {0.0};
    
    int hour[MAX_ARRAY] = {0};
    
    float salary[MAX_ARRAY] = {0.0}; 
     
    void get_Pay_Rate (int id[], float pay_Rate[]);
    
    int main()
    
    {
    
    int i = 0;
    
    get_Pay_Rate(id, pay_Rate);
    
    for (i = 0; i < MAX_ARRAY; i++)
    {
    printf("%.2f", pay_Rate[i]);
    }
    
    _getch;
    
    return 0;
    
    }//END OF MAIN
    
    
    void get_Pay_Rate (int id[], float pay_Rate[])
    {
    	
    
    	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; i++)
    	{
    			for (emp_Count = 0; emp_Count < MAX_ARRAY; emp_Count++)
    			{
    			printf("\n\nPlease enter the pay rate that matches this Id, %d\n\n", id[emp_Count]);
    			scanf("%f", &pay_Rates);
    				if (pay_Rates < 6.0) 
    				{
    					printf("You have entered an invalid payrate; please try again!\n\n");
    					break;
    				}
    				else 
    				{
    				pay_Rate[i] = pay_Rates;
    				}
    		}
    	}
    
    }  // End of get_payRate
    Last edited by GCNDoug; 04-25-2007 at 01:37 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think I understand what you're trying to do, though I'm not certain. Is this what you want?
    Code:
    void get_Pay_Rate (int id[], float pay_Rate[])
    {
    	
    
    	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; i++)
    	{
    			for(;;)
    			{
    			printf("\n\nPlease enter the pay rate that matches this Id, &#37;d\n\n", id[i]);
    			scanf_s("%f", &pay_Rates);
    				if (pay_Rates < 6.0) 
    				{
    					printf("You have entered an invalid payrate; please try again!\n\n");
    					//break;
    				}
    				else 
    				{
    				pay_Rate[i] = pay_Rates;
    				break;
    				}
    		}
    	}
    
    }  // End of get_payRate
    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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    No that's appears to also be an endless loop and isn't incrementing the id number like mine was.

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Is this what you want? It looks like you're looping way too much and reusing i when you want to continue on an error and really do nothing:
    Code:
    // D. Burke (Noir)
    #include <stdio.h>
    
    
    #define MAX_ARRAY 7
    
    
    void get_payrate( int id[], float payrate[], int size );
    void put_payrate( int id[], float payrate[], int size );
    
    
    int main( void ) {
      int id[MAX_ARRAY] = {1111, 2222, 3333, 4444, 5555, 6666, 7777};
      float payrate[MAX_ARRAY] = {0};
    
      get_payrate( id, payrate, MAX_ARRAY );
      printf( "The IDs and Payrates are:\n" );
      put_payrate( id, payrate, MAX_ARRAY );
    
      return 0;
    }
    
    
    void get_payrate( int id[], float payrate[], int size ) {
      int i = 0;
    
      for ( i = 0; i < size; ) {
        printf( "ID: %d\tPayrate: ", id[i] );
        fflush( stdout );
    
        if ( scanf( "%f", &payrate[i] ) == 1 ) {
          if ( payrate[i] < 6.0 )  {
            printf( "invalid payrate\n" );
            continue;
          } else {
            ++i;
          }
        }
      }
    }
    
    
    void put_payrate( int id[], float payrate[], int size ) {
      int i;
    
      for ( i = 0; i < size; i++ ) {
        printf( "ID: %d\tPayrate: %.2f\n", id[i], payrate[i] );
      }
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by GCNDoug View Post
    No that's appears to also be an endless loop and isn't incrementing the id number like mine was.
    It is incrementing i:
    Code:
    for (i = 0; i < MAX_ARRAY; i++)
    And it isn't an endless loop:
    Code:
    break;
    That was just one way of coding it. You could easily use do and while(x>=6.0) etc.

    [edit] In short, what it does is: for each id, gets the pay_rate. Continues getting the pay_rate while it is an invalid rate. I thought that was what you wanted.

    Or see Noir's code.[/edit]
    Last edited by dwks; 04-25-2007 at 01:45 PM.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Code:
    void put_payrate( int id[], float payrate[], int size ) {
      int i;
    
      for ( i = 0; i < size; i++ ) {
        printf( "ID: &#37;d\tPayrate: %.2f\n", id[i], payrate[i] );
      }
    }
    Wow, that works pretty well but the print still isn't working. Also why did you use size, instead of the global for the size of the array?

    I'm prob not going to able to use that code lol cause we haven't used some of that stuff in class. Does anyone see why mine was an endless loop?
    Last edited by GCNDoug; 04-25-2007 at 01:54 PM.

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Wow, that works pretty well but the print still isn't working.
    Then it doesn't work pretty well, does it? How isn't it working? It takes the payrate that matches the id and prints them in pairs, just like they were entered. If that's not what you want, be really specific because I don't understand.
    Also why did you use size, instead of the global for the size of the array?
    So that you're not tied to that macro all over your code if you decide to do something different. Each function should be independent of the rest of the code if possible.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    After it takes all of the id's and pay rates it's not printing them at all; the command program just closes. lol

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps you need to look at this? http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Just add this to the end of main() and it should work:
    Code:
    while(getchar() != '\n');
    getchar();
    [edit] Noir has the same idea. [/edit]
    Last edited by dwks; 04-25-2007 at 02:00 PM.
    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.

  12. #12
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    That's different. The code works fine, your compiler just tricks you into thinking it doesn't by closing the window right away. Add the blue parts:
    Code:
    int main( void ) {
      int id[MAX_ARRAY] = {1111, 2222, 3333, 4444, 5555, 6666, 7777};
      float payrate[MAX_ARRAY] = {0};
    
      get_payrate( id, payrate, MAX_ARRAY );
      printf( "The IDs and Payrates are:\n" );
      put_payrate( id, payrate, MAX_ARRAY );
    
      while ( getchar() != '\n' );
      getchar();
    
      return 0;
    }

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Thanks a lot I figured modified your code just a little bit to change the some of the stuff we don't really use in class. All it need liked you said was the _getch(). Also is this "fflush( stdout );" just clearing the buffer?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > _getch()
    getchar() is preferable if you don't mind hitting the enter key, because it is standard and _getch() is not.

    Also is this "fflush( stdout );" just clearing the buffer?
    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.

    [edit] My connection must be a fraction of a second faster than Noir's. [/edit]

    [edit=2] Even if you were using _getch(), don't forget to add the parentheses. [/edit]
    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.

  15. #15
    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.

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