Thread: Need help with a little problem with pointers.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Need help with a little problem with pointers.

    Hi, I'm new here and relatively new to the C language.

    Anyway I'll get straight to the point.
    I'm working on an assignment ( no I am not asking anyone to help me complete it), it is basically a Launderette system and I'm encountering a slight problem when i tried to use pointers.

    The program stopped working at a certain point which i commented.

    Here is the coding:

    Code:
    ....
    float handWash(){
    
    
        int numOfHWash;
        float hWashTotal;
        float price_hWash = 5;
    
    
        printf("\nEach item hand washed will be RM%.2f", price_hWash);
        printf("\n\nEnter number of clothes to be hand washed: ");
        scanf("%d", &numOfHWash);
    
    
        hWashTotal = numOfHWash*price_hWash;
    
    
        serviceType(&hWashTotal);
    	
        printf("\n\nThe total cost is: RM%.2f", hWashTotal);
    }
    
    ....
    
    void serviceType(float *svcCost){
    
    
        int sType_input;
    
    
        printf("\n\n");
        printf("1. Regular Service: 30%% Discount.\n");
        printf("2. Express Service: 50%% Extra Charge.\n");
        printf("Enter the service type: ");
        scanf("%d", sType_input);
       
       //the program stopped working after receiving the input
        if(sType_input == 1)
            *svcCost = *svcCost * 0.7;
        else
            *svcCost = *svcCost * 1.5;
    }
    So after I entered 1 or 2, a little window would pop up saying the program would stop working.

    I tried removing the scanf() and leaving only one statement.
    The program when I did and I could work around it to make it so but I want to know why and hoping that there might be a solution to this?
    Perhaps any advice?

    Thanks in advance and sorry if I broke any rules

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    For scanf() you need to supply the address of the variable, you need an ampersand.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    scanf("%d", &numOfHWash);
    Code:
    scanf("%d", sType_input);
    Notice any difference between those two? One of them is missing something.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Thank you jimblumberg and anduril462, I should have opened my eyes a little bigger.
    As stupid as I am feeling right now, I think I'll never forget to put '&'.

    Thank you
    Last edited by raxan; 04-09-2012 at 10:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers
    By Elliott Katz in forum C Programming
    Replies: 5
    Last Post: 04-07-2012, 11:20 AM
  2. Problem with pointers
    By fabiux in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2012, 11:45 AM
  3. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  4. pointers problem
    By toshog in forum C Programming
    Replies: 4
    Last Post: 09-11-2006, 03:17 AM
  5. problem with pointers
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-27-2004, 06:13 PM