School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 307,038 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,221 people online right now. Registration is fast and FREE... Join Now!





Store

The original code I wrote this program for, was used on my laptop that ran slackware 9.1. I wrote it to print off reciepts & end of day sales reports on my HP Laster Jet 4 while selling computer equipment on the road at computer shows.

Submitted By: no2pencil
Actions:
Rating:
Views: 1,827

Language: C

Last Modified: May 18, 2007

Snippet


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define MAX 99
  6.  
  7. int main(void) {
  8.      struct {
  9.           char item[MAX];
  10.         float cost;
  11.         int amount;
  12.     } x[MAX];
  13.     int i=1,t2=0,ts=0,c=0;
  14.     float total=0;
  15.     char store[MAX],buffer[MAX],ch;
  16.     FILE *printer;
  17.     printf("\nEnter Store Name:");
  18.  
  19.  
  20.     while(ch!='\n') {
  21.             if(c>=98){
  22.                  printf("\nOverflow");
  23.                  exit(1);
  24.             }
  25.             ch=getc(stdin);
  26.             store[c]=ch;
  27.             c++;
  28.     }
  29.     c=0;
  30.     ch='\0';
  31.  
  32.  
  33.     while (i<MAX) {
  34.          printf("\nSale for %s", store);
  35.         printf("\nEnter item:");
  36.  
  37.  
  38.         while(ch!='\n') {
  39.              if(c>=98){
  40.                   printf("\nOverflow");
  41.                   exit(1);
  42.              }
  43.              ch=getc(stdin);
  44.              buffer[c]=ch;
  45.              c++;
  46.         }
  47.             for(c=0;c<MAX;c++) {
  48.                  if(buffer[c]!='\n')x[i].item[c]=buffer[c];
  49.         }
  50.         c=0;
  51.         ch='\0';
  52.         printf("Enter cost:");
  53.  
  54.         while(ch!='\n') {
  55.              if(c>=98){
  56.                  printf("\nOverflow");
  57.                 exit(1);
  58.             }
  59.             ch=getc(stdin);
  60.             buffer[c]=ch;
  61.             c++;
  62.        }
  63.        x[i].cost=atof(buffer);
  64.        c=0;
  65.        ch='\0';
  66.        printf("How many:");
  67.  
  68.        while(ch!='\n') {
  69.              if(c>=98){
  70.                  printf("\nOverflow");
  71.                  exit(1);
  72.                  }
  73.                  ch=getc(stdin);
  74.                  buffer[c]=ch;
  75.                  c++;
  76.        }
  77.        x[i].amount=atoi(buffer);
  78.        c=0;
  79.        ch='\0';
  80.  
  81.        if(x[i].amount==99) {
  82.                  //i--;
  83.  
  84.                  if ((printer = fopen("LPT1","wb+")) == NULL) {
  85.                  printf("Error trying to connect to printer");
  86.             }
  87.             printf("\n\nSales Summery for %s", store);
  88.             printf("\nTotal Transactions: %i\nItems sold: %i\nTotal profit: $%.2f", ts, t2, total);
  89.             fprintf(printer,"\n\r\n\rSales Summery for %s", store);
  90.             fprintf(printer,"\n\rTotal Transactions: %i\n\rItems sold: %i\n\rTotal profit: $%.2f", ts, t2, total);
  91.             i=1;
  92.  
  93.             while(i<=ts) {
  94.                  fprintf(printer,"\n\rSale #%i: %s\n\r Quantity: %i\n\r Total Sale: $%.2f", i, x[i].item, x[i].amount, x[i].cost*x[i].amount);
  95.                 printf("\nSale #%i: %s\n Quantity: %i\n Total Sale: $%.2f", i, x[i].item, x[i].amount, x[i].cost*x[i].amount);
  96.                 i++;
  97.            }
  98.            exit(0); /* clean smooth exit! End of day, relax */
  99.            if(printer)fclose(printer);
  100.       }
  101.  
  102.       if((printer=fopen("LPT1","wb+"))==NULL){
  103.            printf("Error trying to connect to printer");
  104.       }
  105.       fprintf(printer,"\n\r%d of %s sold for $%.2f\n\r\n\r", x[i].amount, x[i].item, x[i].cost*x[i].amount);
  106.       printf("\n%d of %s sold for $%.2f\n\n", x[i].amount, x[i].item, x[i].cost*x[i].amount);
  107.       if(printer)fclose(printer);
  108.       t2=x[i].amount + t2;
  109.       total=total+(x[i].cost*x[i].amount);
  110.       ts=i;
  111.       i++;
  112.   }
  113.   if(printer) fclose (printer);
  114.   return 0;
  115. }

Copy & Paste


Comments


Linkowiezi 2008-12-15 05:38:37

In a bunch of your if statements, wouldn't it be better to use the 'MAX'/'MAX-1' value instead of 98 and 99? But you maybe not want to correct this if it's supposed to be your original code that you used before. But then this comment might help someone else wanting to use the snippet istead :)


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month