Thread: Receipt Machine in C

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    Receipt Machine in C

    My receipt machine is given the standard input:

    Code:
    10/10/08 12:00
    A1:2.31:1:p
    2b:1.98:2:g
    A1:2.31:2:P
    where the first line is the time and the other lines are purchases (item name: price: quantity: type of tax)

    I am supposed to intialize my arrays according to some format. I tried initializing my arrays (already declared as static) using:
    Code:
    void init(int id)
    {
    extern int cashier, num, quantity[NUM];
    extern double price[NUM];
    extern char tax_type[NUM], name[NUM][2], time[SIZE];
    
    cashier = id;
    strcpy(time, "00/00/00 00:00");
    price[NUM] = 0;
    tax_type[NUM] = 'U';
    name [NUM] = '\0';  /*not sure how to intialize?*/
    }
    but then when i run the program, all the arrays are not intialized, instead it has a random value.

    To purchase an item, I am supposed to use sscanf and getline (getline returns i) in a function to read the standard input and store it into static arrays. I am not sure how to relate sscanf and getline so I did something like this, but the first line didn't even work.
    Code:
    void purchase(void)
    {
    extern double price[NUM]; 
    extern int quantity [NUM]; /* item quantity */
    extern char tax_type[NUM]; /* tax on item */
    extern char name [NUM][2]; /* item name */
    extern char time [SIZE]; /* transaction time */
    extern int num; /* number of distinct items purchased */
    
    sscanf("%s", &time);
    sscanf("%c:%f:%d:%c", &name[], &price, &quantity, &tax_type);
    }
    There are so many holes that i don't even know where to begin. I don't know how to store something in a two dimensional array for name.

    I am a beginner to C, so any help would be appreciated. =)
    Last edited by subwaybusker; 10-21-2008 at 12:13 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    extern is not useful to you in this context. Remove all those, compile and link again.

    Moreover, something like this will work a bit better,
    Code:
    scanf("%s", time);
    scanf("%s:%f:%d:%s", name[0], &price, &quantity, &tax_type);
    but you should probably read more here. You seem confused about the *scanf family... for example, sscanf requires two arguments: first, the input string, and second, a format string. I'm not entirely sure what's best for you in this program either, but you'll probably find this helpful as well. Don't avoid reading the article due to its title.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This thread was in reality far less interesting than I expected it to be.... Citizen's comments should all be addressed. Particularly when it comes to:

    Code:
    sscanf("%c:%f:%d:%c", &name[], &price, &quantity, &tax_type);
    ^ Those uncompileable usages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finite State Machine
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 11:59 AM
  2. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  3. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  4. Turing Machine
    By Brad C in forum Tech Board
    Replies: 2
    Last Post: 08-05-2005, 08:22 PM
  5. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM