Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
JadeEyes
Group Title
..C Programming Help..!!
The question :
A supermarket program
With the use of file create a list of 20 products minimum available in your supermarket.
The program must enable the cashier to check out a customer, verify if product is available and the total sales.
While checking out a customer, you must be able to remove an item and print the receipt of the customer also.
 one year ago
 one year ago
JadeEyes Group Title
..C Programming Help..!! The question : A supermarket program With the use of file create a list of 20 products minimum available in your supermarket. The program must enable the cashier to check out a customer, verify if product is available and the total sales. While checking out a customer, you must be able to remove an item and print the receipt of the customer also.
 one year ago
 one year ago

This Question is Open

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
My attempt: #include<stdio.h> #include<stdlib.h> int main () { int Op1,Op2,productcode,i,j; int array1[i][2]; FILE *fp1; fp1=fopen("CodeProductPrice.txt","r"); printf("\tWelcome to the NMV Chocolate Shop\n"); printf("\n"); printf("Choose an option: \n 1.Checkout Customer\n 2.Search Products\n 3.Exit\n"); scanf("%d",&Op1); switch(Op1) { case 1:{ printf("Choose an option: \n 1.Enter Product Code\n 2.Remove Product\n 3.Print Receipt\n"); scanf("%d",&Op2); switch(Op2) { case 1:{ printf("Terminate by a '.'"); printf("\n"); do { printf("Enter product code: "); scanf("%d",&productcode); /*Here i just can't figure out how to get it to print the product name and price */ for(i=0;i>0;i++) { for(j=0;j<2;j++) { /* Place product name and price into array1[i][j] */ } } } while(productcode!='.') break; } case 2:{ /* The remove product also i have no idea how to do it*/ break; } case 3:{ printf("\t NMV CHOCOLATE SHOP\n"); printf("\t Candy Street\n"); printf("\t Sweets World\n"); for(i=0;i>0;i++) { for(j=0;j<2;j++) { printf("%d\t",array1[i][j]); printf("\n"); } printf("\n"); printf("Thank you for your visit\n Have a nice day !! \n Hope to see you soon !! \n ^_^\n"); } break; } default:{ printf("Error\n"); break; } } break; } case 2:{ /*That too I do not know > Searching of products*/ break; } case 3:{ break; } default:{ printf("Error\n"); break; } } system("pause"); return 0; }
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
Problems are encountered are found in between /* */..There are 4 in all..!! But if you found any other irregularities or improvments..Please do tell..!! I know I haven't yet included the total sales by cashier yet..And the total amount of money customer must pay..and also change to be handed back..^_^..!! I do theses afterwards when i finally be able to know how to get the price from the file..:)..!! File contents found in comment just below..!! Thank you..!!
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
File was done on Naotepad like that : 000NMV_Kit_Kat_2_Fingers12.00 001NMV_Kit_Kat_4_Fingers24.00 002NMV_Kit_Kat_Chunky24.50 003NMV_Kit_Kat_4_Fingers_White_Chocolate24.00 004NMV_Kit_Kat_4_Fingers_Dark_Chocolate24.00 005NMV_Cadbury_Mini_Rolls_Blackcurrant_&_Vanilla15.00 006NMV_Cadbury_Mini_Rolls_Caramel15.00 007NMV_Cadbury_Mini_Rolls_Cherry_&_Vanilla15.00 008NMV_Cadbury_Mini_Rolls_Double_Choc15.00 009NMV_Cadbury_Mini_Rolls_Lemon15.00 010NMV_Cadbury_Mini_Rolls_Milk_Chocolate15.00 011NMV_Cadbury_Mini_Rolls_Strawberry15.00 012NMV_Cadbury_Mini_Rolls_Sweet_Cranberry_&_Vanilla15.00 013NMV_Dairy_Milk_Double_Choc20.00 014NMV_Dairy_Milk_Hazelnut20.00 015NMV_Dairy_Milk_Duo20.00 016NMV_Dairy_Milk_Peppermint20.00 017NMV_Dairy_Milk_Macadamia20.00 018NMV_Dairy_Milk_Fruit_&_Nut20.00 019NMV_Dairy_Milk_Mint_Chips20.00
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
By the way..I'm a complete novice in files and arrays..^^'..!!
 one year ago

tyteen4a03 Group TitleBest ResponseYou've already chosen the best response.0
Not a C++ expert but a crash course: Setting a value in the array: array[elementnum] = somevariable Getting a value from the array: moarvar = array[elementnum] See http://www.cplusplus.com/doc/tutorial/arrays/ for more indepth information.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
Do you know how to use C structs? Ultimately the problem I find with your code is that your data is not organized very well. Never name something 'array'.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
I'd love to help you out but seems you're not here.
 one year ago

tyteen4a03 Group TitleBest ResponseYou've already chosen the best response.0
@wio Try leaving your answer here. :) Also, it looks like the question asked him to use arrays so not sure if struct should be used here.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
If you can't use structs, you want an array for each data point pertaining to a product. You can't use a multidimensional array here because you have different datatypes. You're also going to have to use dynamic memory, since there is no way to know how many products there are, and how long the names of each product will be.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
You want to break up your program into functions as well. Don't tell me you can't use functions....
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
@wio & @tyteen4a03 ..I know how to use functions..^_^..!! I'll try breaking it into functions to make it Less bulky...However.. Arrays can be Okay..My real Problem is FILE I just can't understand a thing about it....How to use it and all..:/...!!
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..If you have any Ebooks or website...Please give me the link..Thank you..^_^..!!
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
Well you start with opening file with fopen() obviously. Then you wanna use fgets() to get one line at a time from the file.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
Do you understand malloc()?
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
are you allowed to use structs or is that just not going to happen?
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..Hello..!! :)..!! Okay for my first problem.. Where i just can't figure out how to get it to print the product name and price.. In the file, it's like that : 015NMV_Dairy_Milk_Duo20.00 I type the product ..015..Then it should print out the name and price.. How can i achieve this..??
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..I can use malloc..!! ^_^..!!
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
Okay, what you want to do is store the product names
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
And price..:P
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..If fgets() will print the one line...So if i create anothe file with the names and price only... Entering 15..It will read line 15 of file and print it..Is that it..??
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
fgets will fetch one line
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
then you sort of have to parse it.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
@JadeEyes here's what I think you should do, try making a function which calls 'fgets()' and counts how many lines there is in the file.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
then use that when you malloc() each array, so they'll be of the correct size.
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..I'm kind of confused..o.O..!! You mean..Make a function which will count the number of times i used fgets..??
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
Basically.... call fgets() on the file until you reach end of file
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
then do something like: int product_count = count_lines();
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
char** product_names = malloc(sizeof(char *) * product_count)
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
float* prices = malloc(sizeof(float) * product_count)
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
these are arrays.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
You could also have the first line in the be a number of how many products there are... then you wouldn't have to count lines.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
If you want to use a static array, then there has there has to be a maximum number of products...
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
....The person can take only 1 or all 20 products...Use this functin { FILE *fp = fopen("c:\\product.txt","w"); if(fp){ fwrite(fp,"ProductNameQuantity TakenPrice\n"); fwrite(fp,"ProductNameQuantity Takenprice\n"); //...................continue for the rest fclose(fp); int product_count = count_lines(); char** product_names = malloc(sizeof(char *) * product_count); float* prices = malloc(sizeof(float) * product_count); } Kind of like that..??
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
okay, now you're confusing me
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
Before you even show the person a member, you should load all of the data about the products into an array.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
show the person a menu
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
you're going to open a file regardless
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
You want to store the data in your arrays. You don't want to open/read/write to the file every time. That is too slow.
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..Okay..!! So basically..I just have to place all the data in the arrays..using fgets and the functions you gave me..Right..?? Then..When the customer have made his choice and come up to the cashier to checkout... Using the first array done above..We can get access to both the name and price that We save his choice in another array..!! Right..??
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
*such that We can save his choice..
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
I didn't give you working code, the general idea.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
once the program exits, you can write all the data back into the file.
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
...Okay..i fink I'll try it this way..^_^..!! Thank you..:)..!! Next how to remove a product from the customerchoice array... Here..I don't know how to proceed at all..!! I thought about using a pointer to get acess to the data then..delete it...But is there a delete option in C programming..??
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
Well, it depends on how you want it to be handled.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
what is the customerchoice array for?
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..customerchoice array is the one where the products the person chose will be stored
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
okay, so to remove an entry, just replace it with the entry that comes next and replace the next one with the one after it, and so on until you reach the end.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.2
you're going to have to store the number of choices made, so you know when you reach the end.
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
...Hmmm...Okay..I'll try to do that way..If i encounter any problem..I'll get back to you again..^_^'...!! I guess that's it..Thank you for your help..^_^..!!
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
..I'll keep it open..!! ^_^
 one year ago

JadeEyes Group TitleBest ResponseYou've already chosen the best response.0
The question i mean..:)
 one year ago
See more questions >>>
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.