# Newbie question - Shopping program

• 07-30-2012
CurlyBrace
Newbie question - Shopping program
So uh...

We were asked to do a program in C that is similar to a shopping program that also keeps track of the nutritional info of what you buy...

Something like this:
Choose 4 of the Fruits and Vegetables you want to buy
[1] Option 1
...
[10] Option 10

Then its supposed to display the added up nutritional info of the 4 foods the user chooses

I've got the solutions on how to add them up... I just dont know how to go on with the program itself.

it goes something like this
(also, my apologies for the imminent cluttered coding)
Code:

```#include <stdio.h> #include <conio.h> /*[0] is always the serving weight   [1]-[7] nutritional value*/ typedef float arrFruit[8]; typedef float arrOne[8]; /*Function displays 10 fruits and/or vegetables + their nutritional information*/ void marketoptions(arrFruit fruit) {   int ex, i;   float fhun = 100.0;   float hund;   arrOne ahundred;                   /*Uses the ahundred array to get the nutritional info per 100g of the food*/      for(ex = 1 ; ex<=8 ; ex++)   ahundred[ex] = (100.0/fruit[0]);                                  for(i = 1 ; i<=7 ; i++)    printf("%9.2f", ahundred[i]*fruit[i]);   printf("\n");   } /*computes servings per grams/other*/ void servingscompute(arrFruit fruit, float grams) {   float result;     result = grams/fruit[0];   printf("%.2f grams is equivalent to %.2f servings\n", grams, result); } /*Function for nutrient information. One fruit/veg per run.*/ void nutrientinfo(arrFruit fruit, float grams) {     int i, ex;     arrOne ahundred;     printf("%9s %8s %8s %8s %8s %8s %8s\n", "Fol(mg)", "Ir(mg)",           "Zi(mg)", "Ca(mg)", "A(IU)", "C(mg)", "E(mg)");            for(ex = 1 ; ex<=8 ; ex++)   ahundred[ex] = (grams/fruit[0]);                                  for(i = 1 ; i<=7 ; i++)    printf("%9.2f", ahundred[i]*fruit[i]);   printf("\n");      } /*goes to market. other options may be available here.. */ void gomarket(arrFruit aPapaya, arrFruit aStraw, arrFruit aSpinach, arrFruit aGrapefruit,                   arrFruit aMango, arrFruit aCherry, arrFruit aArtich, arrFruit aOkra, arrFruit aBroc, arrFruit aKiwi) {     } /*Program hub. starts and returns here when done*/ int main() {     int number, choice, monthspreg, chs;     float serving, result;         /*arrays of fruits*/     arrFruit aPapaya = {78.5, 29, 0.20, 0.06, 16, 746, 47.8, 0.24};     arrFruit aStraw = {72, 17, 0.30, 0.10, 12, 9, 42.3, 0.21};     arrFruit aSpinach = {90, 131, 3.21, 0.68, 122, 9433, 8.8, 1.87};     arrFruit aGrapefruit = {123, 16, 0.10, 0.09, 27, 1414, 38.4, 0.16};     arrFruit aMango = {82.5, 35, 0.13, 0.07, 9, 893, 30, 0.74};     arrFruit aCherry = {244, 20, 3.34, 0.17, 27, 1840, 5.1, 0.56};     arrFruit aArtich = {120, 107, 0.73, 0.48, 25, 16, 8.9, 0.23};     arrFruit aOkra = {80, 37, 0.22, 0.34, 62, 226, 13, 0.22};     arrFruit aBroc = {78, 84, 0.52, 0.35, 31, 1207, 50.6, 1.13};     arrFruit aKiwi = {69, 17, 0.21, 0.10, 23, 60, 64, 1.01};         /*characters that dont really have a serious use other than placeholders etc*/     char dumper, end;             /*Gains important information from the user that may be needed later on*/     printf("Welcome!\n");     printf("First thing's first. How many months are you in pregnancy?: ");     scanf("%d%c", &monthspreg, &dumper);             /*main part of program. stops the loop when indicated by user*/     do{       if (monthspreg > 9)/*prevents invalid input on monthspreg*/         printf("Invalid input.\n");               else{       printf("\n[1] View available selections\n");       printf("[2] Compute Servings from food\n");       printf("[3] Compute Nutrient Info of Food\n");       printf("[4] Go to market!\n");       printf("What do you want to do? ");       scanf("%d%c", &choice, &dumper);                       if(choice==1)/*View available selections*/         {         printf("\nHere are the fruits and vegetables you can choose from:\n");           /*Table*/           printf("%11s %9s %8s %8s %8s %8s %8s %8s\n", "Food", "Fol(mg)", "Ir(mg)",           "Zi(mg)", "Ca(mg)", "A(IU)", "C(mg)", "E(mg)");         printf("%11s", "1.Papaya");          marketoptions(aPapaya);         printf("%11s", "2.Strwberry");          marketoptions(aStraw);         printf("%11s", "3.Spinach");          marketoptions(aSpinach);         printf("%11s", "4.Grapefrt");          marketoptions(aGrapefruit);         printf("%11s", "5.Mango");          marketoptions(aMango);         printf("%11s", "6.Cherry");          marketoptions(aCherry);         printf("%11s", "7.Artichoke");          marketoptions(aArtich);         printf("%11s", "8.Okra");          marketoptions(aOkra);         printf("%11s", "9.Broccoli");          marketoptions(aBroc);         printf("%11s", "10.Kiwi");          marketoptions(aKiwi);         }                 if(choice == 2)/*Compute servings*/         {         printf("Which fruit/vegetable do you want to calculate servings for? (1-10) ");         scanf("%d%c", &chs, &dumper);         if(chs > 10 || chs < 1)         {             printf("Invalid Option\n");         }         else         {         printf("Input amount of food in grams: ");         scanf("%f%c", &serving, &dumper);         /*sends the correct fruit/vegetable info to the servingscompute function*/           switch(chs)           {           case 1: servingscompute(aPapaya, serving);                   break;           case 2: servingscompute(aStraw, serving);                   break;           case 3: servingscompute(aSpinach, serving);                   break;           case 4: servingscompute(aGrapefruit, serving);                   break;           case 5: servingscompute(aMango, serving);                   break;           case 6: servingscompute(aCherry, serving);                   break;           case 7: servingscompute(aArtich, serving);                   break;           case 8: servingscompute(aOkra, serving);                   break;           case 9: servingscompute(aBroc, serving);                   break;           case 10: servingscompute(aKiwi, serving);                        }         }         }                 if(choice == 3)/*Compute nutrition*/         {         printf("Which fruit/vegetable do you want to calculate nutrients for? (1-10) ");         scanf("%d%c", &chs, &dumper);         if(chs > 10 || chs < 1)         {             printf("Invalid Option\n");         }         else         {          printf("Input amount of food in grams: ");         scanf("%f%c", &serving, &dumper);         /*works the same as option 2*/         switch(chs)           {           case 1: nutrientinfo(aPapaya, serving);                   break;           case 2: nutrientinfo(aStraw, serving);                   break;           case 3: nutrientinfo(aSpinach, serving);                   break;           case 4: nutrientinfo(aGrapefruit, serving);                   break;           case 5: nutrientinfo(aMango, serving);                   break;           case 6: nutrientinfo(aCherry, serving);                   break;           case 7: nutrientinfo(aArtich, serving);                   break;           case 8: nutrientinfo(aOkra, serving);                   break;           case 9: nutrientinfo(aBroc, serving);                   break;           case 10: nutrientinfo(aKiwi, serving);                        }         }         }                 if (choice == 4)/*Go to market*/         {             gomarket(aPapaya, aStraw, aSpinach, aGrapefruit, aMango, aCherry, aArtich, aOkra,                     aBroc, aKiwi);              }                                 } /*end of else statement. keep intact.*/                  printf("%.2f\n", aPapaya [0]);         printf("Anything else? (y/n) ");         scanf("%c%c", &end, &dumper);         printf("\n");         }while(end != 'n') ;             /*exits the program*/     printf("+++++++++HAVE A NICE DAY :D+++++++++\n");     printf("Press enter again to exit\n");     getch();     return 0;     }```
I've been stuck at doing gomarket function which does what I described earlier... I just need some clues as to what I should do next.
thanks~
• 07-30-2012
Can you be a LOT more specific, please? "I don't know how to make the program go on" is just "it doesn't work", in a more verbose form. Not much info there for troubleshooting the program.

First, do you have any compiler warnings or errors? Let's deal with the errors, right away. Please list any you get from the program compiling.

Second, find a specific problem and focus on that. What is it, and what's it doing that's wrong?

We could troubleshoot the entire program, but you wouldn't learn (or get a chance to practice) any troubleshooting skills that way - and that is a critically important skill for every programmer.

I know that's not what you wanted to hear, but welcome to the forum, anyway!
• 07-30-2012
whiteflags
Quote:

We were asked to do a program in C that is similar to a shopping program that also keeps track of the nutritional info of what you buy...
I would be tempted to force the user to buy their stuff before computing the nutritional info and all that other stuff. It would make sense to have all the information on hand for all of the products but, once you get to hundreds of items, displaying all of that information is overloading for mere mortals.

Quote:

I've been stuck at doing gomarket function which does what I described earlier...
My best guess is that this function is supposed to fill a four-element array with the users food choices. It shouldn't be harder than what you already know. You already know what the product list looks like. So if the user enters 1, put an Option 1 into the cart. Keep going until you run out of cart space.
• 07-30-2012
CurlyBrace
Haha Its okay... :D
I kinda didn't know how to put my problem with this program in proper words.
Anyways I'm going to try rephrasing it better:
The program itself actually works.
Its just that i dont know what to do with the gomarket function (option 4 of the 'main' menu). Like what am I supposed to do so that it works like a shopping program that only gets the info of the chosen materials through passing it to a function. (like choosing 4 of the 10 choices)

I'm still thinking of the possibilities and things I could try out to make that work. Just needed an idea or something on how to approach it hence this post :)
• 07-30-2012
Perhaps make it work like a real market:

1) the cashier scans a barcode**, that causes the correct description and price info to be calculated and printed on the receipt.

2) subtotals are calculated

3) any taxable items get their tax calculated and added

4) totals are printed out.

I would use an array of structs for this, that you prototype above main(), and initiate in main() and pass as a parameter to all the other functions. Alternatively, it could be a global array created above main().

Either way, all the data is in the array of structs, for every market item.

Unless there's an obviously better way of doing it, I like the programs to mimic the way we do things in everyday life.

** The barcode would just be an id number - and a struct member, so every item would have a simple ID number. You could use the ID number to look up everything else about any item, in the array of data.
• 07-30-2012
AndiPersti
Code:

```/*[0] is always the serving weight   [1]-[7] nutritional value*/ typedef float arrFruit[8]; typedef float arrOne[8]; /*Function displays 10 fruits and/or vegetables + their nutritional information*/ void marketoptions(arrFruit fruit) {   int ex, i;   float fhun = 100.0;   float hund;   arrOne ahundred;                   /*Uses the ahundred array to get the nutritional info per 100g of the food*/      for(ex = 1 ; ex<=8 ; ex++)   ahundred[ex] = (100.0/fruit[0]);  // when x becomes 8 you are out of bounds                            for(i = 1 ; i<=7 ; i++)    printf("%9.2f", ahundred[i]*fruit[i]);   printf("\n");   }```
There's no need for the array "ahundred" at all because all elements contain the same value (except element 0 which you don't use).

I would also consider merging "marketoptions()" and "nutrientinfo()" because basically marketoptions(fruit) == nutrientinfo(fruit, 100).

Bye, Andreas