Thread: making functions, need help

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    7

    making functions, need help

    I need to make a function that asks for user input and prints it. I need to display name, hours worked, and hourly rate. It is not working and I do not know what I am doing wrong. Can anyone help?
    I wrote this function:
    Code:
    int employeedata(char* ch, float* x, float *y)
    {
    	printf("Enter your name:\n");
    	scanf_s("%s", *ch);
    	printf("Enter your hourly rate:\n");
    	scanf_s("%f", *x);
    	printf("Enter number of hours worked:\n");
    	scanf_s("%f", *y);
    }
    and then I call it in main:
    Code:
    int main()
    {
    float rate[5], hours[5];
    	float overtimerate = 1.5;
    	char name[5][20];
    
    
    	int loop;
    
    
    	
    	for (loop = 0; loop < 5; loop++)
    	{
    		
    		employeedata(&name[loop], &rate[loop], &hours[loop]);
    }
    	system("pause");
    		return 0;
    	}

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    scanf expects the address but you are giving is the value. And the first parameter in employeedata function should be name[loop].

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're also missing parameters from your scanf_s calls.
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    7
    Sorry, I am a complete beginner so I am not so great at this yet. How do I give scanf the address then? I thought I was doing that by using *ch because it's a pointer, or is it, am I doing pointers wrong?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    & is the address of operator.

    Edit: but, in your case just use the pointer to char.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-09-2015, 11:07 PM
  2. Making functions
    By Kool4School in forum C Programming
    Replies: 17
    Last Post: 11-16-2008, 09:22 PM
  3. Making functions execute at certain times
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2007, 11:55 AM
  4. Making a pyramid using recursive functions
    By MikeBahu in forum C++ Programming
    Replies: 7
    Last Post: 07-21-2007, 11:58 PM
  5. making a diamond using functions
    By Nikisha in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2003, 09:05 PM

Tags for this Thread