Thread: Can't transfer the input of the user from last function to the other..need help..

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    18

    Unhappy Can't transfer the input of the user from last function to the other..need help..

    Guys, this program is supposed to let the user choose their option in the menu..

    One of the options is to let the user input stuff and then they will not be allowed to use that option anymore. When they get to the next option, it is supposed to be able to get the last stuff they entered from the last function..

    My problem is that the code doesn't seem to work that way.. can you guys help me with this?? I would be really grateful if someone could help me with this..



    Code:
    void tomarket(char *pick1,char *pick2,char *pick3,char *pick4,  float *grams1,float *grams2,float *grams3,float *grams4)
      /*goes to market.. 
      (note that you can only go there once)*/
      {
       system("CLS");     
            
      
      char dump;
      /*This will show up the fruits*/
       char fruits[10][11]={"pears","figs","watermelons","tamarinds","avocados",
      "okras","celeries","arugulas","endives","lemons"};
     
      
    
    
    printf("\n\n\n" 
        "  |======================GO TO MARKET=====================|\n"
        "  |  Choose which food you want to buy from the market:   |\n"
        "  |Press..                                                |\n"
        "  |        [0] Pear                                       |\n"
        "  |        [1] Figs                                       |\n"
        "  |        [2] Watermelon                                 |\n"
        "  |        [3] Tamarind                                   |\n");
    printf("  |        [4] Avocado                                    |\n"
        "  |        [5] Okra(RAW)                                  |\n"
        "  |        [6] Celery(RAW)                                |\n"
        "  |        [7] Arugula(RAW)                               |\n"
        "  |        [8] Endive(RAW)                                |\n"
        "  |        [9] Lemon                                      |\n"
        "  |=======================================================|\n"
        "Pick your first food:  ");
        scanf("%c%c", &pick1,&dump);
        printf("Enter how many grams you will get for the first:   ");
        scanf("%f%c", &grams1,&dump);
        printf("Pick your second food:  ");
        scanf("%c%c", &pick2,&dump);
        printf("Enter how many grams you will get for the second:   ");
        scanf("%f%c", &grams2,&dump);
        printf("Pick your third food  ");
        scanf("%c%c", &pick3,&dump);
        printf("Enter how many grams you will get for the third:   ");
        scanf("%f%c", &grams3,&dump);
        printf("Pick your fourth food  ");
        scanf("%c%c", &pick4,&dump);
        printf("Enter how many grams you will get for the fourth:   ");
        scanf("%f%c", &grams4,&dump);
        
        
          printf("   You bought:\n");
          printf("   %.1f g of %s\n", grams1,fruits[pick1]);
          printf("   %.1f g of %s\n", grams2,fruits[pick2]);
          printf("   %.1f g of %s\n", grams3,fruits[pick3]);
          printf("   %.1f g of %s\n", grams4,fruits[pick4]);
        
        system("PAUSE");
        system("CLS");
        
        main();
        }
        
       void totalnutribought(char pick1,char pick2,char pick3,char pick4,
                          float grams1,float grams2,float grams3,float grams4, 
                          float pear[], float fig[], float water[], float tama[], 
                          float avo[],float okra[],float cele[],float aru[],
                          float endive[],float lemon[])
        /*Compute Total Nutrients to be gained from bought foods*/
        {float pearinfo [7]={12,0.3,0.18,16,41,7.5,0.21};
         float figinfo  [7]={6,0.37,0.15,35,142,2.0,0.11};
         float waterinfo[7]={5,0.36,0.15,11,865,12.3,0.08};
         float tamainfo [7]={3,0.56,0.02,15,6,0.7,0.02};
         float avoinfo  [7]={30,0.21,0.24,4,55,3.8,0.78};
         float okrainfo [7]={44,0.4,0.3,40,188,10.6,0.18};
         float celerinfo[7]={29,0.16,0.1,32,359,2.5,0.22};
         float aruinfo  [7]={19,0.29,0.09,32,475,3.0,0.09};
         float endinfo  [7]={71,0.41,0.4,26,1084,3.2,0.22};
         float leminfo  [7]={9,0.5,0.05,22,18,44.5,0.13};
         
         
         char fruits[10][11]={"pears","figs","watermelons","tamarinds","avocados",
      "okras","celeries","arugulas","endives","lemons"};
          
         
         
          printf("Food       |  %.1f g %s   %.1f g %s  %.1f g %s  %.1f g %s\n",
                grams1,fruits[pick1],grams2,fruits[pick2],grams3,fruits[pick3],
                 grams4,fruits[pick4]);
          printf("Folic Acid |  %.1f mcg    %.1f mcg   %.1f mcg   %.1f mcg\n");
          printf("Iron       |  %.1f mg     %.1f mg    %.1f mg    %.1f mg\n");
          printf("Zinc       |  %.1f mg     %.1f mg    %.1f mg    %.1f mg\n");
          printf("Calcium    |  %.1f mg     %.1f mg    %.1f mg    %.1f mg\n");
          printf("Vitamin A  |  %.1f I.U.   %.1f I.U.  %.1f I.U.  %.1f I.U.\n");
          printf("Vitamin C  |  %.1f mg     %.1f mg    %.1f mg    %.1f mg\n");
          printf("Vitamin E  |  %.1f mg     %.1f mg    %.1f mg    %.1f mg\n");
        
        
         
          system("PAUSE");
          main();
        }
    int main()
    {system("color 2f");
     int ans;
     char pick1;
     char pick2;
     char pick3;
     char pick4;
     float grams1;
     float grams2;
     float grams3;
     float grams4;
     
     do
     {
    
    
     /*This will show how the user should input ... it is almost like menu*/
    printf("\n\n"
           "                ,-.-.     |                        \n"
           "                | | |,--- |__/  ,--.   ,-.-.,---. \n"
           "                | | |,---||  \ |---'   | | ||---' \n"
           "                ` ' '`---^`   ``---'   ` ' '`---' \n"
           "                  |   |          |    |    |         \n"
           "                  |---|,---.,---.|    |--- |---.,   .\n"
           "                  |   ||---',---||    |    |   ||   |\n");
    printf("                  |   |`---'`---^`---'`---'`   '`---|\n"
           "                                                `---'\n"
           "\n\n\n"
           "+++++++++++++++++++++++++++MAIN MENU++++++++++++++++++++++++++++++\n"
           "    Press [1]      Preview Market Selections\n"
           "          [2]      Compute number of servings from amount of food\n"
           "          [3]      Compute Nutrient Information of given good\n"
           "                   and corresponding amount\n");
    printf("          [4]      Go to Market\n"
           "          [5]      Compute Total Nutrients to be gained from\n"
           "                   bought foods\n"
           "          [6]      Feedback\n"
           "          [7]      New Day\n"
           "          [8]      Exit\n");
           scanf("%d",&ans);/*makes users pick*/
           
      switch(ans)
     {
        case 1: preview();break;
        case 2: servings();break;
        case 3: comnutrinfo();break;
        case 4: tomarket(&pick1,&pick2,&pick3,&pick4,&grams1,
         &grams2,&grams3,&grams4);break;
        case 5: totalnutribought(pick1,pick2,pick3,pick4,grams1,
         grams2,grams3,grams4);break;
        case 6: servings();break;
        case 7: servings();break;
        case 8: {system("ClS");
                 printf("\n\n\n"
                        "             ########  ##    ## ######## ####\n" 
                        "             ##     ##  ##  ##  ##       ####\n" 
                        "             ##     ##   ####   ##       ####\n" 
                        "             ########     ##    ######    ## \n" 
                        "             ##     ##    ##    ##           \n" 
                        "             ##     ##    ##    ##       ####\n" 
                        "             ########     ##    ######## ####\n");
                 system("EXIT");break;
                }
        default:{printf("Please enter the right option..\n");
                 system("PAUSE");
                 main();break;
                }
        }
      
       }   
        while(ans!=8);
      
        system("PAUSE");
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My problem is that the code doesn't seem to work that way..
    That's understandable since the code doesn't even to seem compile. Other than that why doesn't the program seem to work some way?

    Jim

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Since all your parameters are already pointers, you don't want the ampersand in front of them in the scanf's:

    scanf("%c%c", & pick1,&dump);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    Oh i see.. i keep getting errors: array subscript is not an integer & too few arguments to function `totalnutribought'.. umm.. how do I take 'em away???? I'm still kinda new to this..

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Why are you calling main from each of your functions? C does have a return statement for a reason.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    Um, can you please explain why i shouldn't call main from each functions??? Sorry im still new to this C programming..

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    i keep getting errors: array subscript is not an integer
    In C array subscripts must be integral numbers, for instance, int, unsigned int, long, unsigned long. You can't use floating point numbers, or structures for this subscript.
    too few arguments to function `totalnutribought'.
    You must pass the correct number and type of variables to your functions.

    You also must include the include files that define the standard functions you are trying to use. You have not supplied any include files so you shouldn't be using functions like system(), printf() etc.. If you want to use these functions then you need to look up the documentation for these functions and include the proper include file.

    Also in future when you are receiving compile errors post the complete error messages, exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the errors. You should also be compiling your program much more often to keep the number of errors that you need to fix to a minimum. I recommend compiling as often as possible, like maybe after every line you type.

    Jim

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by cloudpuffballz View Post
    Um, can you please explain why i shouldn't call main from each functions??? Sorry im still new to this C programming..
    main should be called only once by the host environment (when you execute the program). It should be considered a "special" function because it is the entry point to your program.

    Besides main's special status, you also introduce unnecessary recursion. After making several choices the call stack looks something like this (the last call is at the bottom):

    main // called by the system
    totalnutribought
    main
    tomarket
    main
    tomarket // nothing in your menu prevents the user from selecting this more than once
    main
    tomarket
    main
    tomarket
    main
    totalnutribought
    ...

    This recursion renders your do...while loop in the main function pointless, but worse than that, if the user wants to exit they have to select option 8 as many times as they selected other options in order to unwind the call stack back to the first call to main and then to return from that initial call (which exits the program).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare user input and function with array and bubblesort
    By Gil Carvalho in forum C Programming
    Replies: 15
    Last Post: 06-15-2012, 01:32 PM
  2. Replies: 4
    Last Post: 08-31-2009, 11:04 AM
  3. Replies: 4
    Last Post: 06-04-2009, 01:36 AM
  4. How to form transfer function with 2 input ?
    By axedia in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2006, 12:41 AM
  5. How do I allow a user to input a function?
    By bananasinpajama in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2005, 11:11 AM

Tags for this Thread