A small application of cash-register

This is a discussion on A small application of cash-register within the C Programming forums, part of the General Programming Boards category; Hi, I am making a small application of a cash register that add's items,get total amount etc. My add function ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    21

    A small application of cash-register

    Hi,

    I am making a small application of a cash register that add's items,get total amount etc. My add function does not work and neither does the total function. If someone could help me it will be greatly appreciated.

    Code:
    char itemName;
    float itemPrice=0;
    float total;
    int numberOfItems=0;
    int number;
    char scanItem;
    float scanPrice;
    void add(float itemPrice,char itemName);
    float getTotal();
    float calaverage();
    int main(int argc, char *argv[]){
    
    
    while (1) {
        printf("1. Add Item\n");
        printf("2. Compute Total\n");
        printf("3. Compute Average\n");
        printf("4. Compute Change\n");
        printf("5. Reset Cash-register\n");
        printf("6. Quit\n");
        printf("Enter Option: ");
        scanf("%d",&number);
    
    if(number==1){
    
        scanf("%f,%c",&scanPrice,&scanItem);
        add(scanPrice,scanItem);
        //printf("%4f %c",itemPrice,scanItem);
    
    }
    
    else if(number==2){
    float totalPrice = getTotal();
      printf("%f",&totalPrice);
    
    }
    else if(number==4){
        printf("you pressed4\n");
    }
    else if(number==5){
        printf("you pressed5\n");
    }
    else if(number==6){
        break;
    }
    else{
         printf("Invalid Option\n");
     }
    
       }
    
    }
    
    void add(float aPrice,char aName){
        itemPrice = aPrice;
        itemName = aName;
        total=total+aPrice;
        numberOfItems=numberOfItems+1;
    }
    
    float getTotal(){
    	return total;
    }
    
    float calaverage(){ total/numberOfItems;}

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i will assume that you are a beginner by the number of issues
    with your code so i'll be gentle. firstly, you didnt include any
    header files in the code you supplied, i think that might have
    been a cut and paste mistake so we'll let that slide. secondly,
    you are breaking a very very very basic program into too many
    functions, the only way you have of dealing with it is to use
    global variables, which (for one reason or another) is not
    considered good practice. you could write code in about 50 lines
    to do all of your operations, maybe even less. thirdly, because
    each menu choice calls its own function, it would be a good idea
    to use switch statements as opposed to if and else statements.
    also, you dont need to use braces for these if only one statement
    is to be executed by the if/else thingy - as visible in the code
    below. finally, you need some style - your naming convention
    of variables is a little bit off (first letter uncapitalised, then
    the first letter of the next word within the variable name is???).
    that alone is forgiveable, but only initialising some of the variables
    and using the full definition of main when it is not used, not
    returning 0 at the end of the program, inconsistent brace and
    text indentations, and also the use of the c++ comment specifier
    all need to be addressed, because before you know it, you
    get asked to write code on a different setup and low and behold,
    you run into a number of errors that you have no idea how to
    solve!

    here is your code, modified and operational. i used strings to
    specify an item name, since your previous code only allowed
    for the entry of a single character. all the same, you don't
    need the name of your items as your code stood, unless you
    wanted to do code to print a receipt, but you really would need
    to approach the whole program in a more advanced way - i would
    use structs, and probably not break into 5 separate functions,
    thus completely avoiding need for global variables.

    i dont want to appear harsh, but you needed to know these
    things. my solution here is not the best possible, there are ways
    that this approach could be improved, but i didnt bother as the
    real way i would approach this is as said before, with a struct.

    Code:
    #include <stdio.h>		/*Needed Header*/
    #include <string.h>     /*to use string functions*/
    
    char itemName [50];
    float itemPrice = 0;
    float total = 0;  /*This needed to be initialised definitely*/
    int numberOfItems = 0;
    int number = 0;
    char scanItem [50];
    float scanPrice = 0;
    
    void add(float aPrice, char aName [50]);
    
    float getTotal();
    
    float calaverage();
    
    int main(void){
    
    /*also just want to point out that spacing your
    code appropraitely will make it much more readable*/
    
    while (1) {
        printf("1. Add Item\n");
        printf("2. Compute Total\n");
        printf("3. Compute Average\n");
        printf("4. Compute Change\n");
        printf("5. Reset Cash-register\n");
        printf("6. Quit\n");
        printf("Enter Option: ");
        scanf("%d",&number);
    
        if(number==1){
    	printf ("\nEnter Price: ");
    	scanf("%f",&scanPrice);
    	printf ("Enter item name: ");
    	scanf ("%s", scanItem);
    	printf ("\n");
    	add(scanPrice,scanItem);
    	}
    
        else if (number == 2)
    	printf ("Total is %f\n\n", getTotal());
    
        else if (number == 3)
    	 printf ("The average is %f\n\n", calaverage ());
    
        else if (number == 6)
    	break;
      
        else
    	printf("Invalid Option\n");
    }
    return 0;
    }
    
    
    
    void add(float aPrice,char aName [50]){
        itemPrice = aPrice;
        strcpy (itemName, aName);
        total=total+aPrice;
        numberOfItems++;
    }
    
    float getTotal(){
        return total;	/*i mean there is no justification for a function like this*/
    }
    
    float calaverage(){
        return total/numberOfItems; /*or this*/
    }
    Last edited by Richie T; 02-01-2006 at 03:19 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    you are breaking a very very very basic program into too many
    functions, the only way you have of dealing with it is to use
    global variables
    Having many functions has nothing to do with needing global variables, and generally it is a good thing to separate logic into functions. I do agree that a function that just returns a global variable is completely pointless.

    you dont need to use braces for these if only one statement
    is to be executed by the if/else thingy
    This is a matter of style and neither way is better than the other. Often it is helpful for new programmers to include braces around all block statments, even one-liners. At the very least it may prevent bugs when additional logic is added, and some find it more readable (myself included).

    The code posted in the second example is formatted better, but I don't think most would consider it correct.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    perhaps i was unclear in my statement in global variables, the
    only way that newbie have of dealling with a number of functions
    that must access the same data is most often through global
    variables. also regarding functions, i do think that it is more
    than possible to break a program into too many functions, as it
    can confuse issues for inexperienced programmers - the add
    function would be the only code that i would even consider
    creating a function for myself, but fair point, that is a matter for
    structure and doesnt have much of a detrimental effect. with
    respect to the solution i proposed, it is most definitely not the
    best way to do it but i wanted to keep the code as similar to
    the authors original work so they can make comparisons and see
    how it works, and also to extend on it more easily. hope this
    clarifies my standpoint.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well, i seems that your add fucntion works fine for me and gives the reults of what u expect. and Richie has some good points for u. and if u could explain a bit more about u'r ptoblem. probabaly could help us work through it better way.

    ssharish2005

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14
    You can handle these type of problems with switch and case commands, it is always easy to handle the optional programmes using switch and case statements.
    Code:
    switch(input)
    {
    case <option>:
    statement
    .
    .
    .
    .
    default :
    statement
    .
    .
    }

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    thanks for a lot of helpful suggestion. I have one more question.
    How do I display all the entries that have been added.

    thanks

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    there lies a problem. the way you have set up individual variables
    means that if a user enters a number of items, only the last
    entered one is saved - previous ones get overwritten. This is
    why structs are the way to go for this application as i mentioned
    before, as the allow you to group all of the data relevant to a
    specific item, namely the price and the name. however since
    you have suggested that you are a beginner, this is probably
    going to be a bit difficult to get your head around.

    http://www.cprogramming.com/tutorial/c/lesson7.html

    what you are looking for is an array of structs, each element
    of this array would then be capable of storing the item name and
    its price. that should point you in the right direction
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14

    small cash register

    Hi daisy_polly,
    Your most welcome.
    To display all the enteries entered by you, you need to store all the records in a seperate data file, for that u need to use file stream and then you can only retrieve all the records and display.


    Thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 03:33 AM
  2. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  3. register variables
    By Draco in forum C Programming
    Replies: 7
    Last Post: 08-21-2002, 01:01 PM
  4. Register
    By sean345 in forum C Programming
    Replies: 7
    Last Post: 05-08-2002, 03:06 PM
  5. Cash Register
    By Hursh in forum C Programming
    Replies: 5
    Last Post: 01-22-2002, 02:36 AM

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