Newbie question - Shopping program

This is a discussion on Newbie question - Shopping program within the C Programming forums, part of the General Programming Boards category; So uh... We were asked to do a program in C that is similar to a shopping program that also ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    6

    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~

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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!

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,701
    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.

    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.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    6
    Haha Its okay...
    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

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 07-30-2012 at 02:00 AM.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Christmas Shopping
    By Dave_Sinkula in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-02-2006, 04:15 AM
  2. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 12:00 PM
  3. a program to create months in a year newbie question
    By robasc in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 04:41 PM
  4. Shopping program
    By barim in forum C Programming
    Replies: 10
    Last Post: 06-01-2004, 09:36 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21