-
Arrays
Trying to pass the array to the 2 different functions. having trouble. PLEASE HELP!!!!!!
After it displays the menu and you make the selection it kicks out to the debugger.
#include<stdio.h>
void displaymenu(void);
int empselection();
int gethoursworked(char);
int getpayrate();
int main()
{
int sel;
char emp[5][15] ={"","Joe Smith", "Jane Smith", "Jane Doe", "Chuck Miller"};
int grosspay;
double netpay;
int hours;
int rate;
displaymenu();
sel = empselection();
if(sel == 0)
return 0;
else if (sel == 1)
emp[1][15];
else if (sel == 2)
emp[2][15];
else if (sel == 3)
emp[3][15];
else if (sel == 4)
emp[4][15];
else
puts("\a\n\nINVALID SELECTION!!!!!\n\n"), main();
hours = gethoursworked(emp[sel][15]);
rate = getpayrate();
grosspay = hours * rate;
netpay = (grosspay * .79);
if (hours ==1)
printf("For %d hour, They will make $%6.2f.\n", hours, netpay);
else
printf("For %d hours, They will make $%6.2f.\n", hours, netpay);
int main();
return 0;}
int gethoursworked(char emp)
{
int hours;
printf("Enter hours worked for %s: ", emp);
scanf("%d", &hours);
return hours;
}
empselection()
{
int sel;
printf("Enter a selection: ");
scanf("%d", &sel);
return sel;
}
int getpayrate()
{
int rate;
printf("What is their pay rate? ");
scanf("%d", &rate);
return rate;
}
void displaymenu()
{
puts("Enter 0 to quit");
puts("What Employee would you like to Calculate Pay for?");
puts("1: Joe Smith");
puts("2: Jane Smith");
puts("3: John Doe");
puts("4: Chuck Miller");
return;}
-
Answer
You can do something like this.
int *function(char *array);
int main()
{
char fname[20];
printf("Please Enter Your First Name: ");
scanf("%s",&fname);
char *ptr;
ptr = fname;
YourFunction(ptr);
}
int *YourFunction(char *array)
{
....do this
return a_pointer;
}
I hope this helps
-
Various problems with parameter passing...
Code:
#include<stdio.h>
#define MAX_EMP 5
// this function is passed the whole emp array
void displaymenu(char emp[MAX_EMP][15] );
int empselection();
// this function is passed just one employee
int gethoursworked(char emp[]);
int getpayrate();
int main()
{
int sel;
char emp[MAX_EMP][15] = {
"","Joe Smith", "Jane Smith", "Jane Doe", "Chuck Miller"};
int grosspay;
double netpay;
int hours;
int rate;
displaymenu(emp);
sel = empselection();
// do your error checking
hours = gethoursworked( emp[sel] );
rate = getpayrate();
grosspay = hours * rate;
netpay = (grosspay * .79);
if (hours ==1 )
printf("For %d hour, They will make $%6.2f.\n", hours, netpay);
else
printf("For %d hours, They will make $%6.2f.\n", hours, netpay);
return 0;
}
int gethoursworked(char emp[])
{
int hours;
printf("Enter hours worked for %s: ", emp);
scanf("%d", &hours);
return hours;
}
int empselection()
{
int sel;
printf("Enter a selection: ");
scanf("%d", &sel);
return sel;
}
int getpayrate()
{
int rate;
printf("What is their pay rate? ");
scanf("%d", &rate);
return rate;
}
void displaymenu(char emp[MAX_EMP][15])
{
int i;
puts("Enter 0 to quit");
puts("What Employee would you like to Calculate Pay for?");
for ( i = 1 ; i < MAX_EMP ; i++ ) {
printf( "%d: %s\n", i, emp[i] );
}
return;
}