Thread: Cannot get program to go to function

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28

    Cannot get program to go to function

    This is a game I used to enjoy playing in high school as immature as it is it's a good exercise in programming that will hopefully help me learn.

    There is one function in this code that I cannot go into. Could someone answer me when I ask why?

    Thanks

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h> /* required for randomize() and random() */
    #include <conio.h>  /* required for clrscr() */
    #include <time.h> 
    
    
    void Drug_buy(char city, double *my_money_ptr, int *my_coke_units_ptr, int *my_heroin_units_ptr, int *my_crack_units_ptr, int *my_pcp_units_ptr, int *my_acid_units_ptr, int *my_pot_units_ptr )
    {
              srand(time(NULL));
              double coke_price = 30000 *(double)rand()/(double)RAND_MAX;
              double heroin_price = 5000 * (double)rand()/(double)RAND_MAX;
              double crack_price = 1000 *(double)rand()/(double)RAND_MAX;
              double pcp_price= 800 *(double)rand()/(double)RAND_MAX;
              double acid_price = 500 *(double)rand()/(double)RAND_MAX;
              double pot_price =100 *(double)rand()/(double)RAND_MAX;
          start_buy :  
              
          printf(" $s City Drug Prices\n", city);
          printf("--------------------------------\n");
          printf(" What do you want to buy?\n");
          printf("1: Coke at %d dollars per unit\n", coke_price);   
          printf("2: Heroin at %d dollars per unit\n", heroin_price);  
          printf("3: Crack at %d dollars per unit\n", crack_price);    
          printf("4: PCP at %d dollars per unit\n", pcp_price);  
          printf("5: Acid at %d dollars per unit\n", acid_price); 
          printf("6: pot at %d dollars per unit\n", pot_price); 
          
          
          int drug_choice_selection;
          char drug_choice[100];
          printf("How Much? \n");
                     
          fgets(drug_choice, sizeof(drug_choice), stdin);
          sscanf(drug_choice, "%d", &drug_choice_selection);  
          
     
       
               switch(drug_choice_selection)
               {
               case( '1' ):
               
                     int coke_choice;
                     char coke_Col_line[100];
                     printf("How Much? \n");
                     
                     fgets(coke_Col_line, sizeof(coke_Col_line), stdin);
                     sscanf(coke_Col_line, "%d", &coke_choice);
                     
                     if(coke_choice*coke_price > *my_money_ptr)
                     {
                     printf("You don't have enough Money!\n");
                     printf("Please try again.....\n");
                     goto start_buy;
                     }
                     else
                     {
                     *my_money_ptr = (*my_money_ptr - (coke_choice*coke_price));
                     *my_coke_units_ptr = coke_choice;
                     }
                     
                     
       
       
               case( '2' ):
                     
                     int heroin_choice;
                     char heroin_Col_line[100];
                     printf("How Much? \n");
                     
                     fgets(heroin_Col_line, sizeof(heroin_Col_line), stdin);
                     sscanf(heroin_Col_line, "%d", &heroin_choice);
                     
                     if(heroin_choice*heroin_price > *my_money_ptr)
                     {
                     printf("You don't have enough Money!\n");
                     printf("Please try again.....\n");
                     goto start_buy;
                     }
                     else
                     {
                     *my_money_ptr = (*my_money_ptr - (heroin_choice * heroin_price));
                     *my_heroin_units_ptr = heroin_choice;
                     }
       
       
               case( '3' ):
                     goto start_buy;
       
       
               case( '4' ):
                     goto start_buy;
    
    
    
    
               case( '5' ): 
                     goto start_buy;
    
    
    
    
               case( '6' ): 
                     goto start_buy;   
    
    
               case( '7'): 
                     goto start_buy;    
       
               default :
                       goto start_buy;
       
      /* try again */
       }   
              
         }
         
    
    
    
    
    
    
    
    
    
    
    int main()
    {
          int my_money;
    	  int my_coke_units;
    	  int my_heroin_units;
    	  int my_crack_units;
    	  int my_pcp_units;
    	  int my_acid_units;
    	  int my_pot_units;
        
          int days_left = 31;    
    while(days_left > 0)
    {
          printf(" Day %d of your quest. Where do you want to go\n", days_left);
          
          printf("----------------------------------------------\n");
          
          printf("1: Chicago\n");   
          printf("2: Detroit\n");  
          printf("3: Disneyland\n");    
          printf("4: Mexico City\n");  
          printf("5: Vatican City\n"); 
          printf("6: Columbo\n");
          
          int city_choice_selection;
          char city_choice[100];
    
    
                     
          fgets(city_choice, sizeof(city_choice), stdin);
          sscanf(city_choice, "%d", &city_choice_selection); 
          
          
          switch( city_choice_selection)
          {
          case( 1 ):   
           void Drug_buy(char city, int &my_money, int &my_coke_units, int &my_heroin_units, int &my_crack_units, int &my_pcp_units, int &my_acid_units, int &my_pot_units );         
           
           
           case( 2 ):
                 
                 
           case( 3 ):
                 
           case( 4 ):
    
    
           default:
                   printf("this is the default\n");
           }
    days_left = days_left - 1;
    
    
    
    
    }    
    
    
    
    
     
       system("pause");                     /* the fix for the collapsing window*/
        return (0);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by psppb View Post
    Code:
          case( 1 ):   
           void Drug_buy(char city, int &my_money, int &my_coke_units, int &my_heroin_units, int &my_crack_units, int &my_pcp_units, int &my_acid_units, int &my_pot_units );
    This statement is not a function call, and in fact it shouldn't even compile (it's valid C++ but invalid C).

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Whew ... don't tell me this compiles for you ...

    I'm wary to tred too deeply into this one, but a few things that are causing the problem:

    Code:
    // function definition
    void Drug_buy(char city,
                  double *my_money_ptr,  // <--- this data type doesn't match the function call
                  int *my_coke_units_ptr,
                  int *my_heroin_units_ptr,
                  int *my_crack_units_ptr,
                  int *my_pcp_units_ptr,
                  int *my_acid_units_ptr,
                  int *my_pot_units_ptr )
    
    // function call
    case( 1 ):   
    void Drug_buy(char city,
                  int &my_money,  // <--- this data type doesn't match the function definition
                  int &my_coke_units,
                  int &my_heroin_units,
                  int &my_crack_units,
                  int &my_pcp_units,
                  int &my_acid_units,
                  int &my_pot_units );
    Furthermore, in the function call:
    - You don't need to indicate the return type of the function (remove "void")
    - You don't need the return type for the variables you are passing to the function
    - There is no character variable 'city' in "main()"
    - ... and even if there were, it would be a single character and not a string (character array)

    Which means in that your function, this:

    Code:
    printf(" $s City Drug Prices\n", city);
    ... would not print a string. It would also not print a string because you have a typo (you meant "%s").

    My advice would be to brush up on functions before continuing this program.

    Also, when you start, type up small bits and make sure it compiles. Add a little more and make sure it compiles. Don't try to type it all out at once and then compile when you're done - this will make the whole process go much smoother.
    Last edited by Matticus; 07-17-2012 at 09:12 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You don't do this to call other functions:
    Code:
    char *fgets(char * city_choice, int sizeof(city_choice), FILE * stdin);
    or this:
    Code:
    int sscanf(const char * city_choice, const char * "%d", ... &city_choice_selection);
    or this:
    Code:
    int printf(const char * "this is the default\n");
    So what makes you think you need to do this?:
    Code:
           void Drug_buy(char city, int &my_money, int &my_coke_units, int &my_heroin_units, int &my_crack_units, int &my_pcp_units, int &my_acid_units, int &my_pot_units );
    Instead of just this?:
    Code:
           Drug_buy(city, &my_money, &my_coke_units, &my_heroin_units, &my_crack_units, my_pcp_units, my_acid_units, my_pot_units);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program using if function !!!!!!
    By ravikanyal11 in forum C Programming
    Replies: 9
    Last Post: 10-24-2011, 09:06 AM
  2. Function Program
    By cutie0507 in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2009, 11:38 PM
  3. Help please with function program.
    By gator6688 in forum C++ Programming
    Replies: 12
    Last Post: 10-01-2007, 11:40 AM
  4. function program help
    By katie2 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 08:27 PM
  5. Function program not working...
    By sirSolarius in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 07:35 PM