Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define num_shoes 10
typedef struct
{
int code;
char name[80];
float price;
char colour [10];
}record;
int authentication (int tries)
{
char comparison[20],junk[80];
char username [20]="";
printf("please enter a valid username. (%d tries left)>",3-tries);
scanf("%s",username);
getchar();
FILE * memberfile =fopen("Memberfile.txt","r");
for (;memberfile != NULL && !feof (memberfile);)
{
comparison[0]=0;
fscanf(memberfile,"%s",comparison);
if(strcmp(comparison,username)==0)
{
fclose(memberfile);
return 1;
}
else fgets(junk,80,memberfile);/*reads any remaining characters
on that line and goes down to the new line*/
}
return 0;
}
int search_code (record array [] ,int seeked)
{
int index =0;
for (index =0; index < num_shoes; index++)
{
if (!array[index].name[0])
continue;/* if the first character of the name is 0 (no name)
then continue forward and skip the comparison. repeat the loop*/
if (array[index].code ==seeked)
return index;
}
return -1;
}
/*the following will search the array for an item using the name as the search field
if the item is not found it returns -1*/
int search_colour (record array[],char colour[])
{
for (int index=0; index<num_shoes;index++)
{
if (strcmp (colour,array[index].colour)==0)
return index;
}
return -1;
}
int search_name (record array[],char name[])
{
for (int index =0; index<num_shoes;index++)
{
if (strcasecmp (name,array[index].name)==0)
return index;
}
return -1;
}
float calculate_change (record item, int index)
{
int number_of_shoes;
float amount_of_money;
printf("\nhow many items do you want?");
scanf("%d",&number_of_shoes);getchar();
printf("cost : $%0.2f\n",item.price*number_of_shoes);
printf("\n how much money are you paying?");
scanf("%f",&amount_of_money);getchar();
printf("\n Your change is :$%.2f\n",amount_of_money-(item.price*number_of_shoes));
}
void display_record (record item, int index)
{
int i;
printf("%-4d %20s\t",item.code,item.name);
printf("%-4.f\t",item.price);
printf("%-4d\t",item.colour);
printf("\n");
}
int main (void)
{
int i, j,number_of_items;
float amount_of_money;
record items [num_shoes] ={0};//initialise all locations to zero
char junk;
int menu_code;
for (j=0; j<3 &&!authentication(j);j++);/*this loops the authentication of the
user and gives him 5 triesto enter a valid
user name*/
if (j==3)
{
printf("you have been booted from the system please restart\n");
getchar();
return -1;
}
else printf("welcome to the system\n");
FILE * f =fopen("shoes.txt","r");
if(!f)
{
printf("the file could not be found, create a file\n");
getchar();
return 1;//in the main function this means there was an error
}
for (i=0; i<num_shoes &&!feof(f);i++);
{
fscanf(f,"%d",&items[i].code);
fscanf(f,"%f",&items[i].price);
fscanf(f,"%s",items[i].colour);
/*the fscanfs above will leave a single space on the line, just like a getchar()
used after a scanf, afgetc must be used here to remove the space otherwise the
name read from the file will have a space at the front of it and the program will
not find the name sought*/
fgetc(f);
fgets(items[i].name,80,f);
/*the above line will pick up a maximum of 80 characters from the file, stopping
only when 80 chractersare picked up OR a newline character is picked up. The newline
remains in the item[i].name string, and must be removed*/
items[i].name [strlen(items[i].name) -1] =0;
/*the line above will use the index 'strlen(items[i].name)' (the length of the name)
minus ONE space,which would be the second last charcter,and sets the end of the string
to that new point,effectively cutting ogg the last character from it*/
}
fclose(f);
do
{
fflush(stdin);/*clear the input buffer BEFORE reading,just in case
there'sanything there left over from the previous run*/
printf("\n\n1 - search on code \n2 -search on colour \n3 - search on name \n4 - finish \n\n>");
getchar();
switch (menu_code)
{
case 1:
{
int shoe_code, index_return;
printf("which shoe code?>");
scanf("%d",&shoe_code);
getchar();
index_return = search_code(items,shoe_code);
if (index_return <0)
{
printf("NOT FOUND!");
break;
}
display_record(items[index_return],index_return);
calculate_change(items[index_return],index_return);
break;
}
case 2:
{
int index_return;
char shoe_colour[10]="";
printf("which shoe colour?>");
scanf("%s",shoe_colour);
index_return=search_name(items,shoe_colour);
if (index_return<0)
{
printf("NOT FOUND!");
break;
}
display_record(items[index_return],index_return);
calculate_change(items[index_return],index_return);
break;
}
case 3:
{
int index_return=0;
char item_name[100] ="";
printf("which item name");
/*A simple scanf will not work here because the names have spaces,therefore we
will use an fgets() with the file as 'stdin'. This represents standard input
and is representative of the console. The line below means get a maximum of 80
characters from the user and stores it in 'item_name' like the previous fgets,
the end must be cut off.*/
fgets(item_name,80,stdin);
item_name[strlen(item_name)-1] =0;
/*The end of a string is represented by a 0 character setting
a character in a string to 0 moves the end of the string*/
index_return =search_name(items,item_name);
if(index_return <0)
{
printf("NOT FOUND!");
break;
}
display_record(items[index_return],index_return);
calculate_change(items[index_return],index_return);
break;
}
case 4:
printf("exting\n");
break;
default:printf("invalid choice, please try again\n\n");
break;
}
system("pause");
system("cls");
}while(menu_code!= 4);
}