Thread: Beginner C programmer, Please Help, Please be patience

  1. #16
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Look at your code:
    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); 
    }
    The parameter is called code. Do you ever use the variable code in that function? No. Therefore, you can get rid of the parameter.

    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.

  3. #18
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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);
    name
    Code:
    [6] += sale;
    	choice = getyn("\nAnother");
    	
    	
    	tot += sale;
    	avg = (tot / 5);
    and here:

    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);
    Right??

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Precisely.

    But (there's always a but) this won't work:
    Code:
    name[code][6] += sale;
    name[code] is a string. Why are you incrementing one of the characters in that string? It might turn 'h' into 'i' or something.

    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.

  5. #20
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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.

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  7. #22
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    What do you mean incrementing the string? by using "+=" or somewhere else?

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes. What you're doing is the same as this:
    Code:
    char s[] = "xax";
    s[1] += 2;
    puts(s);  /* prints "xcx" */
    You'd want to increase the values in the sales[] array like I mentioned above.

    [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.

  10. #25
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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?

  11. #26
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    ok that last post That i made didnt really make since.

    so for the sales array i have :

    Code:
    double totsales[6] = {0};
    when you say you used code as the index, where is the index. Is is the {0}??
    Last edited by leopardforest@g; 04-14-2007 at 03:35 PM.

  12. #27
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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 saletotavg;
        
    int codechoice;

        
    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(nameSIZE);
        
    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 keyerr;
    do
        {
        
    printf("\nPlease enter your code:");
        
    scanf("%d%*c", &key);
        
    err = (key 1) || (key 5);
        if (
    errerror();
        }
    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);

    getchar();
    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 (
    16i++ )
        
    ary[i] = 0;
    }

    /*======================================================*/    
    void report(double ary[], int constant)
      {
        
    int i;
      
     
      
    printf("Realtor            Total Sales\n");
      for (
    16i++)
        {
        
    printf("%2d%22.2lf\n"iary[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!

  13. #28
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  14. #29
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Is this going in the right direction??

    PHP Code:
    {
        
        
        
        
    char name[][7] = {"Larry""Sue  ""Eva  ""Morley""Pete"};
        
    double totsales[6] = {012345};
        
        
    double saletotavgthissale;
        
    int codechoice;

        
    initcosts(totsales);
    do
        {
        
    printf("\n\t\tRocklin Realty\n\n\n");
        
        
    code getcode( );
        
    sale getsales( );
        
    namecode ][6];
        
    totsalescode ] += thissale;
        
    choice getyn("\nAnother");
        
        
        
    tot += sale;
        
    avg = (tot 5);

        
        
        } 

  15. #30
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner c++ programmer compile errors
    By dodo10 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2008, 04:37 PM
  2. [C] - String Manipulation {Beginner Programmer}
    By INFERNO2K in forum C Programming
    Replies: 14
    Last Post: 05-21-2005, 11:34 AM
  3. Beginner Programmer
    By silverjump in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 06:03 PM
  4. Beginner c++ programmer looking for suggestions....
    By Sheshi in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 04:38 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 08:15 AM