CashRegister problem

This is a discussion on CashRegister problem within the C++ Programming forums, part of the General Programming Boards category; I am making a cash register program that sks you for the amount and quantity of the item. I also ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    CashRegister problem

    I am making a cash register program that sks you for the amount and quantity of the item. I also want to put a Subtotal in the bottom of the window. But when I enter a second item... The subtotal resets. I want it to keep adding to the subtotal until no more items are left. How can I achieve this?

    [note:] This is a Console App
    What is C++?

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    I didn't see your code so I can't say exactly what is wrong.

    But since you want to preserve a value and add to it in cycles, it seems to me you are missing there a static variable. Just for fun, change the definition of the variable you have for storing the subtotal to:
    Code:
    static <type> variable_name
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Hmm.... nope.

    Heres the code
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.c>
    #include <windows.h>
    #include <stdio.h>
    
    int count = 0;
    int stop = 0;
    float prc;
    int itmnum = 1;
    int qty;
    
    static float sub;
    
    void subttl();
    void getprc();
    void getqty();
    void title();
    
    
    
    int main (int argc, char *argv[])
    {
       for(int count = 0; count < 20 && stop == 0; count++){
                 do{
                    system("CLS");
                    gotoxy(0,0);
                    title();
                    subttl();
                    gotoxy(10,7);
                    getprc();
                    subttl();
                    gotoxy(10,8);
                    getqty();
                    itmnum = itmnum + 1;
                    }
                    while(stop == 0);
    
                 }
       getchar();
       return 0;
    }
    
    void title()
    {
         textcolor(WHITE);
         cout<<"Cash Register v.2.0b\n"
         <<"_____________________________";
    
    }
    
    void getprc()
    {
         cout<<"Enter Price of item number "<<itmnum<<": "; cin>>prc;
                      if(prc == 0){
                      stop = 1;
                      }
    
    }
    
    void getqty()
    {
         cout<<"Enter Quantity: "; cin>>qty;
    }
    
    void subttl()
    {
       gotoxy(20,17);
       sub = prc*qty;
       cout<<"Subtotal: "<<sub;
    }
    What is C++?

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    You never increment the product number.

    To fix your reseting problem, originally set the subtotal to 0. Also, replace this:

    sub = prc*qty;

    with this:

    sub += prc*qty;

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    THANK YOU

    I didnt even know about +=
    ive notice these

    *=
    -=

    what do they mean?
    What is C++?

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    a+=b is the same as a=a+b

    Same with the rest.

  7. #7
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    When you write var = var op value, you can shorthand it to var op= value

    thus a = a + 1 is the same as a += 1
    or a = a / (12 + b) is the same as a /= (12 + b)

    In the last example the parentesis are still necessary, since the compiler will remove your shorthand and otherwise calculate a / 12 + b
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Vicious,

    The "shorthand" that the other folks have been referring to is really a compiler tool.

    Since C++ compiles in a single pass, innovations to the language were implemented to make the process more efficient.

    The combined assignment operators are an example of abbreviated operations that allow the compiler to compile our source code in one pass more effectively.

    The 'ternary' operator is another good example.

    Code:
    max = (a > b) ? a : b;
    substitutes for:
    Code:
    if (a > b)
         max = a;
    else
         max = b;
    (We think we're being cool... like the other 99.999999% of the world knows what the heck we're doing anyway!)
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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