Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

JadeEyes

..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
  1. JadeEyes
    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; }

    • one year ago
  2. JadeEyes
    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..!!

    • one year ago
  3. JadeEyes
    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

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

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

    • one year ago
  5. tyteen4a03
    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.

    • one year ago
  6. wio
    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'.

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

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

    • one year ago
  8. tyteen4a03
    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.

    • one year ago
  9. wio
    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.

    • one year ago
  10. wio
    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....

    • one year ago
  11. JadeEyes
    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..:/...!!

    • one year ago
  12. JadeEyes
    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..^_^..!!

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

    Ah I'm online

    • one year ago
  14. wio
    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.

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

    Do you understand malloc()?

    • one year ago
  16. wio
    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?

    • one year ago
  17. JadeEyes
    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..??

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

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

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

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

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

    And price..:P

    • one year ago
  21. JadeEyes
    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..??

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

    *another

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

    fgets will fetch one line

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

    then you sort of have to parse it.

    • one year ago
  25. wio
    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.

    • one year ago
  26. wio
    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.

    • one year ago
  27. JadeEyes
    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..??

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

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

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

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

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

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

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

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

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

    these are arrays.

    • one year ago
  33. wio
    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.

    • one year ago
  34. wio
    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...

    • one year ago
  35. JadeEyes
    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..??

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

    okay, now you're confusing me

    • one year ago
  37. wio
    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.

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

    show the person a menu

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

    you're going to open a file regardless

    • one year ago
  40. wio
    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.

    • one year ago
  41. JadeEyes
    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..??

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

    *such that We can save his choice..

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

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

    • one year ago
  44. wio
    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.

    • one year ago
  45. JadeEyes
    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..??

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

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

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

    what is the customerchoice array for?

    • one year ago
  48. JadeEyes
    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

    • one year ago
  49. wio
    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.

    • one year ago
  50. wio
    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.

    • one year ago
  51. JadeEyes
    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..^_^..!!

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

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

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

    The question i mean..:)

    • one year ago
    • Attachments:

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
  • 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.