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

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

    Beginner C programmer, Please Help, Please be patience

    I am very new to the programming world and I am taking C Language programming and I am having trouble with an assignment. This is the assignment....


    Quote Originally Posted by homework
    Rocklin Realty sells homes. They currently employ 5 realtors (real estate sales people) each with their own code (1-5). Their names are: Larry Lister, Sue Sales, Eva Escrow, Morley Money, and Pete Profit. For each sale, a sales person (realtor) enters their code (1-5) and the amount of the sale. They want to track these total sales by realtor. The report they desire is displayed below:

    Realtor Total Sales
    Larry Lister 1200000.00
    Sue Sales 2450000.00
    Eva Escrow 2200000.00
    Morley Money 4200600.00
    Pete Profit 5400870.00

    Average = 3200000.00
    Total = 15487000.00

    Write a program that creates an array to store the sales totals by realtor. The program loops until all sales are entered, each time asking for a sales person code (1-5) and sale amount, and prompting if there is another one. Each sales person sells many homes. The total sales by realtor is the total of all sales by that realtor, not just one sale. When all the sales figures are entered, the report above is displayed.

    Write 3 functions:

    1. main() - define 2 arrays: the array for total sales and the array for the realtor names, and the variables to store the total sales and average sales. It loops until the user responds with ‘N’ to “Another sale (Y/N): “. Within the loop the program prompts for the realtor code (1-5), prompts for the sale amount, updates the sales array, and prompts for another. After all sales are entered, it calls report() and getaverage(). After the report is displayed, and after the getaverage() is called, print the total and average.
    2. report() - receives the sales array and names array as arguments and displays the report of each sales persons total sales. Use pointer syntax. The total and average are NOT displayed in the report function.
    4. getaverage() - receives the sales array and the address of total and average as arguments. Use pointer syntax. It gets the total and average values from the sales array. It does NOT display the total and average. The total and average are displayed from the main() function in the final lines of code.

    Requrements:
    1. Use prototypes, use appropriate header files, and use pointer syntax for report() and getaverage(). No Globals.

    Grading: No bugs, following directions, style, organization, readability, pointer usage.
    I am not asking how to do the entire assignment, but rather one part. I don't know how to get the desired names to display with the sales. I can get it so the program will display 1-5 where the names should be, but I don't know how to display the names as in the example.

    your patience is greatly appreciated.

    leo
    Last edited by Ken Fitlike; 04-13-2007 at 12:49 PM. Reason: changed [code][/code] to [quote][/quote] tags to reduce horizontal extent

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    sorry for the crappy post. i have never posted on here

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, the code tags preserved the formatting. For long quotes, use quote tags.

    Anyway, without reading all of the fine print of the assignment, I would still have to conclude that it contains a lot of details. What you should be doing is working slowly on it.

    Can you state the problem to yourself in simple English or some other eqivalent natural language? Do you intuitively understand the solution to the assignment? If you can answer yes to both of those questions, then on paper or in a simple text editor, try explicitly writing down every single step of your solution.

    After you have done all of this, translate the above solution into C.

    Some of this is already taken care of. They tell you the exact functions to write and what the functions should do. This means you just have to pay attention to their details.

    Lastly, unless you demonstrate that you put forth effort, you are not likely to be helped, and in fact, quite likely to be flamed.

    I am not asking how to do the entire assignment, but rather one part. I don't know how to get the desired names to display with the sales. I can get it so the program will display 1-5 where the names should be, but I don't know how to display the names as in the example.
    Somehow I think you're having trouble with more than this. If you're using printf() to display your information, then use %s in place of the names, and pass the names of the arrays. For example, if you have all of your names of the employees in an array called names and their salaries in an array called salaries, then perhaps the following would work:

    Code:
    for(i=0;i<5;i++)
    {
    	printf("%s %d\n",names[i],salaries[i]);
    }
    This is only a demonstration of an idea of how it could work. You should write your own code and post it.
    Last edited by MacGyver; 04-13-2007 at 01:07 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    You could have a char array containing the names and save them in the same order and it should be rather easy to print the names.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    this is what i have at the moment. Is this the right idea? more to come...




    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*=========================Functions=========================*/
    
    void main( );
    void report( );
    void getaverage( );
    double getchoice(double ch);
    
    
    
    
    
    
    /*==============================================================*/
    void main( )
    {
    	double totalsales[40];
    	char realnames[6][6] = {"Larry", "Sue", "Eva", "Morley", "Pete"};
    	
    	double totsale;
    	double avgsale;
    	double ch;
    	
    	
    	
    	
    	printf("\n\t\tRocklin Realty\n\n\n");
    
    	getchoice(ch);
    	
    	printf("%s\n%s\n%s\n%s\n%s\n%s\n", realnames[0],realnames[1],realnames[2],realnames[3],realnames[4],realnames[5]);
    		
    
    
    
    		
    
    return;
    }
    /*==============================================================*/
    double getchoice(double ch)
    {
    	printf("Please enter your code:");
    	scanf("%lf%*c", ch);
    return (ch);
    }
    /*==============================================================*/
    
    
    
    /*==============================================================*/
    
    
    
    /*==============================================================*/
    
    void report( )
    {
    
    
    
    }
    /*==============================================================*/
    void getaverage( )
    {
    
    
    
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    void main() is not the right idea. It's non-standard. You should be using int main(), no matter what your teacher says. See the FAQ.

    Code:
    scanf("&#37;lf%*c", ch);
    You're missing a &. Also, do you really want to be using a double?
    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. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    #5
    I dont think you want to store numbers in a char array

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Here is my latest. I appreciate your help. Now i must go to work. be back later

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*=========================Functions=========================*/
    
    int main( );
    void report( );
    void getaverage( );
    double getchoice(double ch);
    double getsales(double sale[20]);
    int getyn(char msg[]);
    
    
    
    
    /*==============================================================*/
    int main( )
    {
    	
    	double totalsales[40];
    	char realnames[6][7] = {"Larry", "Sue", "Eva", "Morley", "Pete"};
    	
    	double totsale;
    	double avgsale;
    	double ch, money;
    	int choice, sale, i;
    	
    do
    	{	
    	
    	printf("\n\t\tRocklin Realty\n\n\n");
    
    	getchoice(ch);
    	getsales(sale);
    	totalsales[sale];
    	
    	for (i = 0; i <5; i++)
    		printf("%s  %.2lf\n", realnames[i], totalsales[i]); 
    		
    	
    	
    	choice = getyn("\nAnother");
    	}
    while (choice != 'N');
    		
    
    return 0;
    }
    /*==============================================================*/
    double getchoice(double ch)
    {
    	printf("Please enter your code:");
    	scanf("%lf%*c", &ch);
    return (ch);
    }
    /*==============================================================*/
    double getsales(double sale[20])
    {
    	double dollar;
    	
    	printf("Please enter your sale:");
    	scanf("%lf%*c", &dollar);
    	
    return (dollar);
    }
    
    /*==============================================================*/
    
    
    
    /*==============================================================*/
    
    void report( )
    {
    
    
    
    }
    /*==============================================================*/
    void getaverage( )
    {
    
    
    
    }
    /*==============================================================*/
    int getyn(char msg[])
    {
    	int c;
    printf("%s (Y/N):", msg);
    
    c = getchar();
    c = toupper(c);
    return c;
    }
    /*==============================================================*/

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not bad; but you call this function
    Code:
    double getchoice(double ch)
    {
    	printf("Please enter your code:");
    	scanf("&#37;lf%*c", &ch);
    return (ch);
    }
    like this:
    Code:
    getchoice(ch);
    I think you should either get rid of the parameter, return the value, and same the result (don't discard the return value); or pass the variable as a pointer.

    Also, this line doesn't do anything:
    Code:
    totalsales[sale];
    (And sale is uninitialized because the call to getsales() doesn't modify it; also, it is the wrong type for getsales().)

    You only need to prototype main() for really old, non-standard compilers.

    I suggest you turn on warnings. Add these options to GCC:
    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.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    I have been working on this program for the last three days and yesterday I some great help from this site. Thank you to those that helped me. But I have run into another brick wall.

    Here is my current code. I have the display that I want, but I can't seem to get the sales that I input to go to the names in the display. Note: the function getaverage is still in the works.



    PHP Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>


    #define SIZE 6
    /*======================================================*/    
        
    int getyn(char *msg);

    int getcode(int *code);
    double getsales(double *money );

    void error(void);
    void initcosts(char *ary);
    void report(char ary[][7], double constant);


    /*======================================================*/
    /*======================================================*/    
    int main(void )
    {
        
        
        
        
    char name[6][7] = {"Larry""Sue  ""Eva  ""Morley""Pete"};
        
    double totsales[6];
        
        
    double saletotavg;
        
    int codechoice;

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

        
        
        }
    while (
    choice != 'N');    
    printf("%.2lf\n\n"sale); // To check to see if it is printing the right amount
        
        
    report(name, *totsales);
        
        
    printf("\nTotal:%.2lf"totsales);
        
    printf("\nAverage:%.2lf"avg);

    return 
    0;
    }

    /*======================================================*/    
    /*======================================================*/    
    void getaverage(double *totsalesdouble *tot  )
    {
        return;
    }
    /*======================================================*/    
    int getcode(int *code)
    {
    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 *money )
    {
        
    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(char *ary)
    {
        
    int i;

        
    for (
    16i++ )
        
    ary[i] = 0;
    }

    /*======================================================*/    
    void report(char ary[][7], double constant)
      {
        
    int i;
      
     
      
    printf("Realtor\t\t\t\t\tTotal Sales\n\n");
      for (
    05i++)
        {
        
    printf("%s\t\t%22.2lf\n"ary[i], i);
        }
      
      }    
    /*======================================================*/    
    /*======================================================*/ 


    Thank for the help it is greatly appreciated.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want to colour code use codeform (with the default options) instead of PHP tags: http://dwks.theprogrammingsite.com/m...e/cfonline.htm

    Doesn't this sample look better?
    Code:
    totalsales[sale];
    /*======================================================*/ 
    /*======================================================*/     
    int main(void ) 
    { 
         
         
         
        char name[6][7] = {"Larry", "Sue  ", "Eva  ", "Morley", "Pete"}; 
        double totsales[6]; 
         
        double sale, tot, avg; 
        int code, choice; 
    
        //initcosts(totsales); 
    do 
        { 
        printf("\n\t\tRocklin Realty\n\n\n");
    Or in Dev-C++ style:
    Code:
    totalsales[sale];
    /*======================================================*/ 
    /*======================================================*/     
    int main(void ) 
    { 
         
         
         
        char name[6][7] = {"Larry", "Sue  ", "Eva  ", "Morley", "Pete"}; 
        double totsales[6]; 
         
        double sale, tot, avg; 
        int code, choice; 
    
        //initcosts(totsales); 
    do 
        { 
        printf("\n\t\tRocklin Realty\n\n\n");
    (Don't listen to me, I wrote codeform. )

    return statements in void functions are optional.

    You're not calling get_code properly.
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'll expand on "You're not calling get_code properly." get_code(), as you can see, takes an int* for an argument:
    Code:
    /*======================================================*/     
    int getcode(int *code) 
    { 
    int key, err; 
    do 
        { 
        printf("\nPlease enter your code:"); 
        scanf("&#37;d%*c", &key); 
        err = (key < 1) || (key > 5); 
        if (err) error(); 
        } 
    while (err); 
    
    
    return (key); 
    }
    But you're calling it with a char**:
    Code:
        char name[6][7] = {"Larry", "Sue  ", "Eva  ", "Morley", "Pete"}; 
    
        // ...
         
        code = getcode(name);
    Why don't you just leave out that parameter? You're not using it.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    I am not sure how I am supposed to call get_code. And do you mean here

    Code:
    code = getcode(name);
    Now is this because it is a pointer?
    So do I need to have it like:

    Code:
    code = getcode(*name);

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    oh ok maybe i should have read your last reply before posting my last

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by leopardforest@g View Post
    I am not sure how I am supposed to call get_code. And do you mean here

    Code:
    code = getcode(name);
    Now is this because it is a pointer?
    So do I need to have it like:

    Code:
    code = getcode(*name);
    No, no. getcode() takes an int*, so maybe you'd want to pass it
    Code:
    code = getcode(&code);
    or something; but since you never use the parameter, why not get rid of it?

    Unless you have to use that prototype. In that case, don't return code; instead, use the code passed as a parameter.
    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.

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