Show 80 post(s) from this thread on one page
Page 3 of 3 First 123
• 04-14-2007
dwks
Code:

`char name[][100] = {"Larry Lister", "Sue Sales  ", "Eva Escrow  ", "Morley Money", "Pete Profit"};`
I'm guessing that you added those extra spaces so that when you print the names they'll all be the same length. Well, you can achive the same thing with this:
Code:

`printf("&#37;10s\n", str);`
It makes the string at least 10 characters wide, padding it with spaces.

Code:

```void getaverage(double totsales[], double *tot, double *avg ) {         double total, average;                 totsales[] += tot; }```
I'm guessing you want to calculate the total an average of all of the elements in totsales. In that case, you don't need those local variables, but you do need at least one for loop and the number of elements in totsales (since this is a constant, 6, you don't really have to pass it to the function).

To calculate the total, add up all of the elements in the array. To calculate the average, take the total and divide that by six.

[offtopic] How do you like codeform? [/offtopic]
• 04-14-2007
leopardforest@g
Alright this what I have at the moment and it is giving me what I want, now I just need to make sure that it is what I am supposed to do. Here is it is.

Code:

```#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> /*======================Functions==========================*/        int getyn(char *msg); int getcode(void); double getsales(void); void error(void); void initcosts(char *ary); void report(char ary[][100], double *constant); void getaverage(double *sales, double *num1, double *num2); /*======================================================*/ /*======================================================*/        int main( ) {         char name[][100] = {"Larry Lister", "Sue Sales  ", "Eva Escrow  ",                 "Morley Money", "Pete Profit"};         double totsales[6] = {0};         double sale, tot, avg;         int code, choice; do         {         printf("\n\t\tRocklin Realty\n\n\n");                 code = getcode( );         sale = getsales( );         totsales[ code - 1 ] += sale;         choice = getyn("\nAnother");                 } while (choice != 'N');                report(name, totsales);                 getaverage(totsales, &tot, &avg);                 printf("\nTotal=\t\t\t\t%22.2lf", tot);         printf("\nAverage=\t\t\t%22.2lf\n", avg); return 0; } /*======================================================        */ void getaverage(double *sales, double *num1, double *num2) {         int i;  for (i = 0; i < 5; i++)         {         *num1 += *(sales + i);         *num2 += (*(sales + i) / 5);         } } /*======================================================*/        void report(char ary[][100], double *constant)   {     int i;         printf("\n\nRealtor\t\t\t\t\tTotal Sales (\$)\n\n");   for (i = 0; i < 5; i++)     {     printf("%s\t\t%22.2lf\n", ary[i], *(constant + i));     }   }        /*======================================================*/ int getcode(void) { int key, err; do         {         printf("\nPlease enter your code:");         scanf("%d%*c", &key);         err = (key < 1) || (key > 5);         if (err) error();         } while (err); return (key); }        /*======================================================*/        double getsales(void) {         double dollar;         printf("Please enter your sale:\$");         scanf("%lf%*c", &dollar); return (dollar); } /*======================================================*/        int getyn(char *msg) {         int c; printf("%s (Y/N):", msg); c = getchar( ); c = toupper(c); return c; } /*======================================================*/        void error(void) {         printf("\n\tYour code is invalid\n");         printf("\tPlease try another code"); } /*======================================================*/        void initcosts(char *ary) {         int i; for (i = 1; i < 6; i++ )         ary[i] = 0; } /*======================================================*/```

yes codeform works very nice, makes the code much easier to read.

Oh and do i need all of 4 of the header files?
• 04-14-2007
dwks
You need:
• stdio.h for scanf() and printf()
• ctype.h for toupper()

I don't see any functions that would require string.h (like strcmp, strcat, etc), nor stdlib.h (like rand() etc).

Code:

`printf("&#37;s\t\t%22.2lf\n", ary[i], *(constant + i));`
scanf() uses %f for floats and %lf for doubles, but printf() uses %f for both types.
• 04-14-2007
leopardforest@g
so using
Code:

` 22.2lf`
is not desired?

so use
Code:

` 22.2f`
right? I think I have had this problem before.
• 04-15-2007
Salem
• 04-15-2007
dwks
leopardforest@g has stopped using PHP code tags. I convinced him/her to use codeform instead. I figured that codeform was better than php tags if one wanted coloured code . . . If you don't like codeformed code, well . . . rats. I think it looks nice. :)

Quote:

so using
Code:

` 22.2lf`
is not desired?

so use
Code:

` 22.2f`
right? I think I have had this problem before.
It's not right if you're using floats. But if you're using doubles, then you do need to use the l. (For scanf() only, of course.)
Show 80 post(s) from this thread on one page
Page 3 of 3 First 123