A community for students.
Here's the question you clicked on:
 0 viewing
JadeEyes
 2 years ago
..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.
JadeEyes
 2 years ago
..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.

This Question is Open

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0My 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; }

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0Problems 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..!!

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0File 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

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0By the way..I'm a complete novice in files and arrays..^^'..!!

tyteen4a03
 2 years ago
Best ResponseYou've already chosen the best response.0Not 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.

wio
 2 years ago
Best ResponseYou've already chosen the best response.2Do 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'.

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

tyteen4a03
 2 years ago
Best 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.

wio
 2 years ago
Best ResponseYou've already chosen the best response.2If 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.

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

JadeEyes
 2 years ago
Best 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..:/...!!

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0..If you have any Ebooks or website...Please give me the link..Thank you..^_^..!!

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

wio
 2 years ago
Best ResponseYou've already chosen the best response.2Do you understand malloc()?

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

JadeEyes
 2 years ago
Best 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..??

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0..I can use malloc..!! ^_^..!!

wio
 2 years ago
Best ResponseYou've already chosen the best response.2Okay, what you want to do is store the product names

JadeEyes
 2 years ago
Best 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..??

wio
 2 years ago
Best ResponseYou've already chosen the best response.2then you sort of have to parse it.

wio
 2 years ago
Best 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.

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

JadeEyes
 2 years ago
Best 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..??

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

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

wio
 2 years ago
Best ResponseYou've already chosen the best response.2char** product_names = malloc(sizeof(char *) * product_count)

wio
 2 years ago
Best ResponseYou've already chosen the best response.2float* prices = malloc(sizeof(float) * product_count)

wio
 2 years ago
Best ResponseYou've already chosen the best response.2You 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.

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

JadeEyes
 2 years ago
Best 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..??

wio
 2 years ago
Best ResponseYou've already chosen the best response.2okay, now you're confusing me

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

wio
 2 years ago
Best ResponseYou've already chosen the best response.2you're going to open a file regardless

wio
 2 years ago
Best ResponseYou've already chosen the best response.2You 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.

JadeEyes
 2 years ago
Best 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..??

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0*such that We can save his choice..

wio
 2 years ago
Best ResponseYou've already chosen the best response.2I didn't give you working code, the general idea.

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

JadeEyes
 2 years ago
Best 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..??

wio
 2 years ago
Best ResponseYou've already chosen the best response.2Well, it depends on how you want it to be handled.

wio
 2 years ago
Best ResponseYou've already chosen the best response.2what is the customerchoice array for?

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0..customerchoice array is the one where the products the person chose will be stored

wio
 2 years ago
Best ResponseYou've already chosen the best response.2okay, 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.

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

JadeEyes
 2 years ago
Best 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..^_^..!!

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0..I'll keep it open..!! ^_^

JadeEyes
 2 years ago
Best ResponseYou've already chosen the best response.0The question i mean..:)
Ask your own question
Sign UpFind more explanations on OpenStudy
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.