Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

JadeEyes

  • one year 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
  1. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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; }

  2. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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..!!

  3. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    File was done on Naotepad like that : 000-NMV_Kit_Kat_2_Fingers-12.00 001-NMV_Kit_Kat_4_Fingers-24.00 002-NMV_Kit_Kat_Chunky-24.50 003-NMV_Kit_Kat_4_Fingers_White_Chocolate-24.00 004-NMV_Kit_Kat_4_Fingers_Dark_Chocolate-24.00 005-NMV_Cadbury_Mini_Rolls_Blackcurrant_&_Vanilla-15.00 006-NMV_Cadbury_Mini_Rolls_Caramel-15.00 007-NMV_Cadbury_Mini_Rolls_Cherry_&_Vanilla-15.00 008-NMV_Cadbury_Mini_Rolls_Double_Choc-15.00 009-NMV_Cadbury_Mini_Rolls_Lemon-15.00 010-NMV_Cadbury_Mini_Rolls_Milk_Chocolate-15.00 011-NMV_Cadbury_Mini_Rolls_Strawberry-15.00 012-NMV_Cadbury_Mini_Rolls_Sweet_Cranberry_&_Vanilla-15.00 013-NMV_Dairy_Milk_Double_Choc-20.00 014-NMV_Dairy_Milk_Hazelnut-20.00 015-NMV_Dairy_Milk_Duo-20.00 016-NMV_Dairy_Milk_Peppermint-20.00 017-NMV_Dairy_Milk_Macadamia-20.00 018-NMV_Dairy_Milk_Fruit_&_Nut-20.00 019-NMV_Dairy_Milk_Mint_Chips-20.00

  4. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    By the way..I'm a complete novice in files and arrays..^^'..!!

  5. tyteen4a03
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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 in-depth information.

  6. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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'.

  7. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    I'd love to help you out but seems you're not here.

  8. tyteen4a03
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  9. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  10. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    You want to break up your program into functions as well. Don't tell me you can't use functions....

  11. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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..:/...!!

  12. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ..If you have any E-books or website...Please give me the link..Thank you..^_^..!!

  13. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Ah I'm online

  14. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  15. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Do you understand malloc()?

  16. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    are you allowed to use structs or is that just not going to happen?

  17. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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 : 015-NMV_Dairy_Milk_Duo-20.00 I type the product ..015..Then it should print out the name and price.. How can i achieve this..??

  18. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ..I can use malloc..!! ^_^..!!

  19. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Okay, what you want to do is store the product names

  20. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    And price..:P

  21. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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..??

  22. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    *another

  23. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    fgets will fetch one line

  24. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    then you sort of have to parse it.

  25. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  26. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    then use that when you malloc() each array, so they'll be of the correct size.

  27. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ..I'm kind of confused..o.O..!! You mean..Make a function which will count the number of times i used fgets..??

  28. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Basically.... call fgets() on the file until you reach end of file

  29. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    then do something like: int product_count = count_lines();

  30. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    char** product_names = malloc(sizeof(char *) * product_count)

  31. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    float* prices = malloc(sizeof(float) * product_count)

  32. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    these are arrays.

  33. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  34. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    If you want to use a static array, then there has there has to be a maximum number of products...

  35. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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,"ProductName-Quantity Taken-Price\n"); fwrite(fp,"ProductName-Quantity Taken-price\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..??

  36. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    okay, now you're confusing me

  37. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Before you even show the person a member, you should load all of the data about the products into an array.

  38. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    show the person a menu

  39. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    you're going to open a file regardless

  40. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  41. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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..??

  42. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    *such that We can save his choice..

  43. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    I didn't give you working code, the general idea.

  44. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    once the program exits, you can write all the data back into the file.

  45. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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..??

  46. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Well, it depends on how you want it to be handled.

  47. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    what is the customerchoice array for?

  48. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ..customerchoice array is the one where the products the person chose will be stored

  49. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  50. wio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    you're going to have to store the number of choices made, so you know when you reach the end.

  51. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 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..^_^..!!

  52. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ..I'll keep it open..!! ^_^

  53. JadeEyes
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The question i mean..:)

  54. Not the answer you are looking for?
    Search for more explanations.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.