J
Jeffrey Barrett
/*
machine.txt:
------------------
Cola
0.75 20
Ruby Red Blast
1.00 10
Lemon Fizz
0.75 8
Grape Soda
0.90 5
Citrus Flip
0.85 0
Habanero Surprise
0.80 11
-------------------
*/
/* ================================================================ */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* ================================================================ */
#define MAXNUMDRINKS 6
#define STRINGSIZE 25
/* ================================================================ */
typedef char String [STRINGSIZE];
typedef enum bool { false, true } bool;
typedef struct drinkRec
{
String brand;
float price;
int quantity;
bool soldOut;
} drinkRec;
typedef drinkRec drinkList [ MAXNUMDRINKS ];
/* ================================================================ */
void ReadAllDrinks ( int* numDrinks, drinkList drinks );
/* ================================================================ */
int main ( )
{
drinkList drinks;
int numDrinks;
printf ("Welcome to the Soda Machine.\n\n");
ReadAllDrinks ( &numDrinks, drinks );
return ( 0 );
}
/* ================================================================ */
void ReadAllDrinks (int* numDrinks, drinkList drinks )
{
String dummy;
char *line;
FILE* inFile;
int count = 0;
inFile = fopen ( "machine.txt", "r" );
while (line != NULL)
{
line = fgets(drinks[count].brand,STRINGSIZE,inFile);
drinks[count].brand [strlen (drinks[count].brand) - 1] = '\0';
fscanf(inFile,"%f%d",&drinks[count].price,&drinks[count].quantity);
fgets(dummy,STRINGSIZE,inFile);
count++;
}
*numDrinks = (count - 1);
fclose ( inFile );
}
machine.txt:
------------------
Cola
0.75 20
Ruby Red Blast
1.00 10
Lemon Fizz
0.75 8
Grape Soda
0.90 5
Citrus Flip
0.85 0
Habanero Surprise
0.80 11
-------------------
*/
/* ================================================================ */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* ================================================================ */
#define MAXNUMDRINKS 6
#define STRINGSIZE 25
/* ================================================================ */
typedef char String [STRINGSIZE];
typedef enum bool { false, true } bool;
typedef struct drinkRec
{
String brand;
float price;
int quantity;
bool soldOut;
} drinkRec;
typedef drinkRec drinkList [ MAXNUMDRINKS ];
/* ================================================================ */
void ReadAllDrinks ( int* numDrinks, drinkList drinks );
/* ================================================================ */
int main ( )
{
drinkList drinks;
int numDrinks;
printf ("Welcome to the Soda Machine.\n\n");
ReadAllDrinks ( &numDrinks, drinks );
return ( 0 );
}
/* ================================================================ */
void ReadAllDrinks (int* numDrinks, drinkList drinks )
{
String dummy;
char *line;
FILE* inFile;
int count = 0;
inFile = fopen ( "machine.txt", "r" );
while (line != NULL)
{
line = fgets(drinks[count].brand,STRINGSIZE,inFile);
drinks[count].brand [strlen (drinks[count].brand) - 1] = '\0';
fscanf(inFile,"%f%d",&drinks[count].price,&drinks[count].quantity);
fgets(dummy,STRINGSIZE,inFile);
count++;
}
*numDrinks = (count - 1);
fclose ( inFile );
}