All pastes #739599 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-10-17 09:54 UTC
rendered paste body
HEADER:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once
#include <stdio.h>
#include <tchar.h>

// TODO: reference additional headers your program requires here








// Exercise 4.cpp : Defines the entry point for the console application.

//*******************************************************************//

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

struct sales //structure for the sales file 
{
int customer_number; 
char item[50];
char part_number[15];
float cost; 
char date[6];
};

sales mysales[200];
// initialise the sales struct

/***********************************************************************
//
// Functions
//
***********************************************************************/

//given to us on the task page
int get_field(FILE *file, char delimiter, char *ptr) 
{ 
	int length=0; 
	char temp; 
	while(1) 
	{
		temp=fgetc(file); 
		if (temp==EOF)
			return -1;

		if((temp==delimiter)||(temp==0x0A))
			{
				ptr[length]='\0';
				return length;
			}
		if(temp>0x1F)
			{ 
				ptr[length]=temp; 
				length++;
			}
	}
	return -1; 
}

//processes the data given through the get_field function
float input(FILE *file, int *customerID, int size)
{
    float total = 0;  //initialises the count array elements to zero. 
    int data; //initialises data for further use
	char buffer[100]; //This array will store each customer's number of purchases
	int id;
	int i, j=0;  
	  
	for (i=0; i<size; i++)
	{
		customerID[i] = 0; //This for loop is used to get each customer number and sets the count for the histogram.
	}

    while(1)
	{
	    data = get_field(file, ',', buffer); // calls the get_field function.

	    if (data == -1) //if data is equal to -1 then it will break out of the loop
	    {
		    break;
	    }
		
		id = atoi(buffer); // convert from ascii to integer 
		mysales[j].customer_number = atoi(buffer);		 //assigns customer number to structure mysales of type sales
		customerID[id] = customerID[id] + 1;  //increments customerID by 1 for histogram
		
		data = get_field(file, ',', buffer); //calls get_function
		for (int u=0; u<50; u++){
			(mysales[j].item[u] = buffer[u]); //assigns item description to structure mysales structure of type sales
			}
		
		data = get_field(file, ',', buffer); //calls get_function
		for (int u=0; u<50; u++){
			(mysales[j].part_number[u] = buffer[u]); //assigns part number to structure mysales of type sales
		}
		
		data = get_field(file, ',', buffer);
		mysales[j].cost = atof(buffer); //assigns cost to structure mysales of type sales

		total = total + mysales[j].cost; //calculates the total as it generates the data and is later displayed. 
	
		data = get_field(file, ',', buffer);
		for (int u=0; u<50; u++){
			(mysales[j].date[u] = buffer[u]); //assigns date to structure mysales of type sales
		}
		
		j++; //increments j until it completes each customer within the sales file.
	}

	return total; //returns the calculated total of sales
}

void sales_summary(int *customerID, int size, float total)
{
	printf("-----------------------------------------------------------------\n\n");
	printf("Total sales for the month: $  %.2f\n\n", total);
	printf("-----------------------------------------------------------------\n\n");

	printf("Customer Purchase Distribution\n\n");

	for(int i=1; i < size; i++) //for loop used to retrieve customer
	{
		printf("customer %03i: ", i); //prints customer ID as an integer with leading zeros. 

		for(int k=0; k < customerID[i]; k++) //for loop used to retrieve the data needed within the histogram.
		{
			printf("X"); //print X upon condition being true.
		}

		printf("\n");
	}
printf("\n");
}


int _tmain(int argc, _TCHAR* argv[])
{
	FILE *sales_in;
	float total;

	int histogram[11]; //initialise histogram array to 11.
     
	sales_in = fopen("sales.csv","r"); //open sales file

	total = input(sales_in, histogram, 11);// sales input 
	sales_summary(histogram, 11, total);// sales summary 

	fclose(sales_in);

	return 0;
}