Sorry, but I am still not sure what you mean when you say "I dont ever use the parameter". Can you explain more. Sorry this is difficult for me to understand.
Sorry, but I am still not sure what you mean when you say "I dont ever use the parameter". Can you explain more. Sorry this is difficult for me to understand.
Look at your code:
The parameter is called code. Do you ever use the variable code in that function? No. Therefore, you can get rid of the parameter.Code:/*======================================================*/ int getcode(int *code) { 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); }
Your compiler will warn you about "unused parameters" if you add these options to the compiler's command line:
Code:-W -Wall -ansi -pedantic
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.
ok I think I am getting it.
so like this right?
[CODE]{
printf("\n\t\tRocklin Realty\n\n\n");
code = getcode( );
sale = getsales(totsales);
nameand here:Code:[6] += sale; choice = getyn("\nAnother"); tot += sale; avg = (tot / 5);
Right??Code:int getcode() { int key, err; do { printf("\nPlease enter your code:"); scanf("%d%*c", &key); err = (key < 1) || (key > 5); if (err) error(); } while (err);
Precisely.
But (there's always a but) this won't work:
name[code] is a string. Why are you incrementing one of the characters in that string? It might turn 'h' into 'i' or something.Code:name[code][6] += sale;
You need another array to record the sales for each person.
[edit] Having an array index called code messes up the formatting . . . consider using [ code ] or [co[b][/b]de]. [/edit]
Last edited by dwks; 04-14-2007 at 02:57 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.
So now my biggest issue (i think) is connecting the entered sale with the "code". So if i entered "1" for code, then "1234" for sale, I need it to print in the Larry row.
I think using the multidimensional array for the names is messing with me.
I dont know what I am doing wrong.
Well, you're halfway there. You have an array in which you can look up the names by index; now you need an array in which you can look up the sales by index. Something like this:
Code:double sale[6] = {0};
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.
What do you mean incrementing the string? by using "+=" or somewhere else?
I just decided to do this for fun, and mainly because I was following directions, I declared to arrays: one for names and one for sales. I just used the code as an index. For every sale a person made, I added that sale to the previously existing value, and I ended up with a running total of that person's sales so far.
As far as the names go, I indexed them in the order they were presented in the document. Nothing complicated.
Yes. What you're doing is the same as this:
You'd want to increase the values in the sales[] array like I mentioned above.Code:char s[] = "xax"; s[1] += 2; puts(s); /* prints "xcx" */
[edit] citizen describes the procedure as well. [/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.
ok when you say you used "code" as an index you mean that when entering 1 it is assigning the whatever to the first name?
ok that last post That i made didnt really make since.
so for the sales array i have :
when you say you used code as the index, where is the index. Is is the {0}??Code:double totsales[6] = {0};
Last edited by leopardforest@g; 04-14-2007 at 03:35 PM.
So here is my first try at the same program and it works exactly like I want except for the fact that I cant get the names to show.
PHP Code:
int getyn(char msg[]);
int getcode();
double getsales( );
void error(void);
void initcosts(double ary[]);
void report(double ary[], int constant);
int main(void )
{
double name[6];
double totsales[6];
double sale, tot, avg;
int code, choice;
initcosts(name);
do
{
code = getcode();
sale = getsales();
name[code] += sale;
choice = getyn("\nAnother");
tot += sale;
avg = (tot / 5);
}
while (choice != 'N');
printf("%.2lf\n\n", sale);
report(name, SIZE);
printf("\nTotal:%.2lf", tot);
printf("\nAverage:%.2lf", avg);
//printf("%s", name[0]);
return 0;
}
/*======================================================*/
/*======================================================*/
void getaverage(double totsales[80], double *tot )
{
return;
}
/*======================================================*/
int getcode()
{
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( )
{
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");
return;
}
/*======================================================*/
void initcosts(double ary[])
{
int i;
for (i = 1; i < 6; i++ )
ary[i] = 0;
}
/*======================================================*/
void report(double ary[], int constant)
{
int i;
printf("Realtor Total Sales\n");
for (i = 1; i < 6; i++)
{
printf("%2d%22.2lf\n", i, ary[i]);
}
}
/*======================================================*/
So I can get one program to show the numbers that I want but not the names and vice versa in the programs that I have listed above. So how can I put them together??? Thanks!
The index is the number between the brackets. I just subtracted one from code, keeping zero length arrays in mind.
For instance, if we got input such that code = 5, I would access it like this
sales_per_person[ code - 1 ] += this_sale;
and since I initialized the names array the way I did, I can use code to also refer to a particular person's name, so printing it is simple.
For example
puts( people[ code - 1 ] );
Last edited by whiteflags; 04-14-2007 at 04:19 PM.
Is this going in the right direction??
PHP Code:
{
char name[][7] = {"Larry", "Sue ", "Eva ", "Morley", "Pete"};
double totsales[6] = {0, 1, 2, 3, 4, 5};
double sale, tot, avg, thissale;
int code, choice;
initcosts(totsales);
do
{
printf("\n\t\tRocklin Realty\n\n\n");
code = getcode( );
sale = getsales( );
name[ code ][6];
totsales[ code - 1 ] += thissale;
choice = getyn("\nAnother");
tot += sale;
avg = (tot / 5);
}
I THINK I AM GETTING IT!!
Can someone please look over my code and tell me what you think. Right now I am working on the getaverage function. Thanks for all of you help so far!!!
Code:Code:#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> /*======================================================*/ int getyn(char *msg); int getcode( ); double getsales( ); void error(void); void initcosts(char *ary); void report(char ary[][100], double constant[]); /*======================================================*/ /*======================================================*/ int main(void ) { 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"); tot += sale; avg = (tot / 5); } while (choice != 'N'); report(name, totsales); printf("\nTotal:%.2lf", tot); printf("\nAverage:%.2lf", avg); return 0; } /*====================================================== void getaverage(double totsales[], double *tot, double *avg ) { double total, average; totsales[] += tot; } /*======================================================*/ 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( ) { 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( ) { 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; }