Beginner C programmer, Please Help, Please be patience

This is a discussion on Beginner C programmer, Please Help, Please be patience within the C Programming forums, part of the General Programming Boards category; Code: char name[][100] = {"Larry Lister", "Sue Sales ", "Eva Escrow ", "Morley Money", "Pete Profit"}; I'm guessing that you ...

  1. #31
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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("%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]
    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.

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

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

  4. #34
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    so using
    Code:
     22.2lf
    is not desired?

    so use
    Code:
     22.2f
    right? I think I have had this problem before.

  5. #35
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Please stop using PHP to colour in your C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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

Page 3 of 3 FirstFirst 123
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, 03:38 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 07:15 AM

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