Array output not as expected

This is a discussion on Array output not as expected within the C Programming forums, part of the General Programming Boards category; I am creating simple program to take input of an employee number (1-5) and how much their paycheck was. I ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    29

    Array output not as expected

    I am creating simple program to take input of an employee number (1-5) and how much their paycheck was. I loop until no more entries are desired. Then, I output their name (based on their employee number -1 matching the name array position) and their accumulated pay. Here is my code:

    Code:
    main()
    {
    	double total[6];
    	char name[5][13] = {"Steve Zahn", "Sarah Palin", "Eva Menedez", "Morley Safer", "Pete Rose"}; 
    	int cnum;
    	double pay;
    	char ch;
    
    	init_total(total);
    
    	do
    		{
    		printf("\nEnter your employee number (1-5): ");
    		scanf("%d%*c", &cnum);
    		printf("\nEnter amount of paycheck: ");
    		scanf("%lf%*c", &pay);
    		total[cnum] += pay;
    		printf("\nDo more (Y/N): ");
    		scanf("%c%*c", &ch);
    		ch = toupper(ch);
    	}
    	while (ch != 'N');
    
    	report(total, name[cnum-1]);
    	return 0;
    }
    void report(double s[], char n[])
    {
    	int i;
    
    	printf("\nEmployee\t\t\tTotal Pay\n");
    	printf("-------\t\t\t-----------\n");
    
    	for (i = 1; i < 6 ; i++)
    		printf("%s\t\t\t%.2lf\n", &n[i], s[i]);
    }
    Here is what my output should be, for example:

    Code:
    Employee               Total Pay
    -------------              -------------
    Steve Zahn             100.00
    Sarah Palin             1234.56
    Eva Menedez          222.22
    Morley Safer           321.09
    Pete Rose               7777.89
    I am doing something wrong and not getting the right output. I am not getting the name array to display properly. It displays similar to this:

    Code:
    Employee               Total Pay
    -------------              -------------
    Steve Zahn             100.00
    teve Zahn               1234.56
    eve Zahn                222.22
    ve Zahn                  321.09
    e Zahn                    7777.89
    Any help would be appreciated.
    Last edited by Futomara; 11-02-2010 at 03:53 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    You don't need the ampersand in your printf

    Code:
    for (i = 1; i < 6 ; i++)
    		printf("%s\t\t\t%.2lf\n", &n[i], s[i]);
    Jim

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Without the ampersand the program crashes.

    There are no global variables allowed either. So the name array is within main().

    So, how do I get the array to enumerate properly? Notice it is only pulling the first element and then lopping off one character at a time. This is very confusing.
    Last edited by Futomara; 11-02-2010 at 05:26 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    Code:
    void report(double s[], char n[])
    {
    	int i;
    
    	printf("\nEmployee\t\t\tTotal Pay\n");
    	printf("-------\t\t\t-----------\n");
    
    	for (i = 1; i < 6 ; i++)
    		printf("%s\t\t\t%.2lf\n", &n[i], s[i]);
    }
    You are treating "n" in the prinf as a if 'n[][]' is being passed.

    The printf should be

    Code:
    		printf("%s\t\t\t%.2lf\n", n, s[i]);
    Jim

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Okay, so now my output is like this:

    Code:
    Employee                 Total Pay
    -------                 -----------
    Pete Rose                    112233.00
    Pete Rose                     332211.00
    Pete Rose                     332211.00
    Pete Rose                     12345.00
    Pete Rose                     54321.00
    So, I must be passing the wrong arguments to the report() function.

    Code:
    report(total, name[cnum-1]);

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    You are only passing one name. You probably want to pass the whole array.

    Code:
    void report(double s[], char n[5][20])
    and the my first answer should be correct.

    Don't forget to change the prototype, function definition and where you are calling the function.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Jim,

    Thank you!! You are helping me understand C programming much better. My instructor isn't that great, obviously, which is why I sought help elsewhere. I seem to be making simple mistakes. I still have more to do on this project, so I may be back for help. It seems that when I hit a roadblock, I am just losing focus out of frustration. I really appreciate your help.

    ~Futomara
    Last edited by Futomara; 11-02-2010 at 06:32 PM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Now, I am converting the arrays to pointers. Hopefully I don't need to start a new thread. Please tell me if I do. When I compile, I get this error:

    warning C4700: uninitialized local variable 'total' used

    I am going over my code again and again and don't understand. I am calling the initialization function. If I change it from:

    Code:
    init_total(total);
    to this:

    Code:
    init_total(*total);
    the compile again fails.

    I'm sure it's something simple I am missing. Here's my code:

    Code:
    void init_total(double *t);
    void report(double *s, char *n[]);
    
    main()
    {
    	double *total;
    	char *name[] = {"Steve Zahn", "Sarah Palin", "Eva Menedez", "Morley Safer", "Pete Rose"};
    	int cnum;
    	double pay;
    	char ch;
    
    	init_total(total);
    
    	do
    		{
    		printf("\nEnter your employee number (1-5): ");
    		scanf("%d%*c", &cnum);
    		printf("\nEnter amount of paycheck: ");
    		scanf("%lf%*c", &pay);
    		*(total+cnum) += pay;
    		printf("\nDo more (Y/N): ");
    		scanf("%c%*c", &ch);
    		ch = toupper(ch);
    	}
    	while (ch != 'N');
    
    	report(total, name);
    
    	return 0;
    }
    void report(double *s, char *n[])
    {
    	int i;
    
    	printf("\nEmployee\t\t\tTotal Sales\n");
    	printf("-------\t\t-----------\n");
    
    	for (i = 1; i < 6 ; i++)
    		printf("%s\t\t\t%.2lf\n", *(n+i-1), *(s+i));
    }
    void init_total(double *t)
    {
    	int i;
    	for (i=0; i < 6; i++)
    		*(t+i) = 0;
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... in main you define total as a pointer... Later you initialize it as an array (incorrectly at that).

    Try this....
    Code:
    // first 
    main()
    {  double total;  
    
    
    // then
    init_total(&total);
    
    
    // later
    void init_total(double *t)
    {
        *T = 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Total is an array. Pointer syntax is to be used. If I am improperly initializing it, then show me how. Your code seems to change total from a pointer to a primitive and it compiles with a list of errors too long to list. Thanks, though.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    You must allocate memory for total either by static allocation or dynamic allocation using malloc/free.

    Either:

    Code:
       double total[10];
       double *ptr_total;
       ptr_total = total;
    Jim
    Last edited by jimblumberg; 11-03-2010 at 10:23 AM. Reason: Removed new/delete code as it is not C

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Jim,

    We were not taught this in class. I tried it and it works. What if I change it like this:

    Code:
    double total[6];
    and keep the other (affected) lines of code like this:

    Code:
    init_total(total);
    
    *(total+cnum) += pay;
    
    report(total, name);
    
    void report(double *s, char *n[])
    {
    	int i;
    
    	printf("\nEmployee\t\t\tTotal Sales\n");
    	printf("-------\t\t-----------\n");
    
    	for (i = 1; i < 6 ; i++)
    		printf("%s\t\t\t%.2lf\n", *(n+i-1), *(s+i));
    }
    void init_total(double *t)
    {
    	int i;
    	for (i=0; i < 6; i++)
    		*(t+i) = 0;
    }
    This works. It compiles without error and runs without error. But, am I using pointer syntax properly?

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    Does it give you the correct results?

    It looks ok to me at first glance.

    Jim

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Yes, I've gone through several runs and it does not crash and returns the proper results.

    Basically, I left everything as is except instead of declaring total as a pointer I keep it as an array. Everything else is converted to pointer syntax.

    So, even if total is an array, doesn't that mean by using pointer syntax in the functions, etc., that each element is treated as a pointer or memory address? Arrays are already passed by address. I think I have it right. What's your opinion?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Futomara View Post
    Jim,
    Code:
    void init_total(double *t)
    {
    	int i;
    	for (i=0; i < 6; i++)
    		*(t+i) = 0;
    }
    This works. It compiles without error and runs without error. But, am I using pointer syntax properly?
    Take note that your loop is only initializing the first 6 bytes of the array if doubles. If total is an array, you need to treat it like one. This should work...
    Code:
    void init_total(double *t)
    {
    	int i;
    	for (i=0; i < 6; i++)
    		*t[i] = 0; 
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21