C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-19-2002, 08:03 PM   #1
Registered User
 
Join Date: Jan 2002
Posts: 2
Thumbs up Structures Arrays and Char Strings

Hey whats up fellows. I'm taking C programming and Anaytical Geometry over a one month winter semester. Yah its a *****. Anyway the i've been having problems finding examples of the prgram we need to create anywhere on the internet. we're asked to incorporate the following facilitating the use of character strings:

Accepts ten employee records (structures) from the screen
employee number
employee name
hours worked
pay rate
Displays the records to the screen with a gross pay calculated from hours worked and pay rate
Displays totals for hours worked and gross pay; and averages for hours worked, pay rate, and gross pay
Sort the records in ascending employee name order.
More than one function must be used (main, input, output) or (main, input, output, sort).

but theres no example of this anywhere on the internet or in the C book , trust me i've looked. heres what i have so far but theres complier erros. i'm so stuck please help.

#include<stdio.h>

struct work {
char emp_number[4];
char name[20];
int hours[5];
int rate[4];
};

struct work list[10];


void main(){

/************
LOOP TO INPUT THE DATA FOR 10 PEOPLE
************/
{
int i, emp_hours, emp_rate;
for(i=0;i<10;i++){

printf("Enter employee number: ");
scanf("%s", list[i].emp_number);
printf("Enter name: ");
scanf("%s", list[i].name);
printf("Enter hours worked: ");
scanf("%d", list[i].hours);
printf("Enter hourly rate: ");
scanf("%d", list[i].rate);
}

printf("\n");
printf("EMP# Name Hours Rate\n");
printf("=== ==== ======= ======\n");
{
/*Calculate Gross Pay */
hours(sum= sum + individual;
#include<stdio.h>
}
}
whats wrong why wont it let me add the elements of the structure please help
Crocksmyworld is offline   Reply With Quote
Old 01-19-2002, 08:49 PM   #2
Registered User
 
Join Date: Jan 2002
Posts: 1
sorry dude i've been programming for years and this is over my head. it would take a true genius to figure this out!!!
hackerr425 is offline   Reply With Quote
Old 01-19-2002, 08:56 PM   #3
Hamster without a wheel
 
iain's Avatar
 
Join Date: Aug 2001
Posts: 1,385
hackerr425 - what is the point of posting a reply to say you dont know the answer???

corcksmyworld - post the error messages thrown up by the compiler and which compiler you are using.
__________________
Monday - what a way to spend a seventh of your life
iain is offline   Reply With Quote
Old 01-19-2002, 09:46 PM   #4
Registered User
 
Join Date: Jan 2002
Posts: 9
iain may be able to help you faster than I can, but as for this part of your program I have made a few small changes and it compiles. You still need to work out the sort, and print routines.


#include<stdio.h>

struct work {
char emp_number[4];
char name[20];
int hours[5];
int rate[4];
};

struct work list[10];


int main(){

/************
LOOP TO INPUT THE DATA FOR 10 PEOPLE
************/
//{ one too many curly brackets
int i, emp_hours, emp_rate;
for(i=0;i<10;i++){

printf("Enter employee number: ");
scanf("%s", list[i].emp_number);

printf("Enter name: ");
scanf("%s", list[i].name);

printf("Enter hours worked: ");
scanf("%d", list[i].hours);

printf("Enter hourly rate: ");
scanf("%d", list[i].rate);
}

printf("\n");
printf("EMP# Name Hours Rate\n");
printf("=== ==== ======= ======\n");
{
/*Calculate Gross Pay */
//hours(sum= sum + individual);
//#include<stdio.h> I dont know why this is here


}
}
mervin is offline   Reply With Quote
Old 01-19-2002, 10:03 PM   #5
Registered User
 
Join Date: Jan 2002
Posts: 2
Thumbs up

Thanks for taking the time out to help Melvin it means a lot to me. How would you go about totaling the elements of the structure for example totaling list[i].hours since hours is what needs to be totaled and it is a member of the structure??
Crocksmyworld is offline   Reply With Quote
Old 01-19-2002, 11:56 PM   #6
Registered User
 
Join Date: Sep 2001
Posts: 752
Hmm, there are a couple problems here...
First, your struct itself...
Code:
struct work { 
char emp_number[4]; 
char name[20]; 
int hours[5]; 
int rate[4]; 
};
Let's start with emp_number. Is this supposed to actully be a number? Because if so, then you should be using an int, not an array of char. Having it as a char[4] only makes sense if the employee number can be something like.. 4B3
Why does each employee have 5 different hour values and 4 different rate values?

Unless you have some special reason for using arrays, change hours to an int, rate to an int, and emp_number to an int. (If you do this, then you'll have to change how you input the emp_number to use scanf("%d", list[i].emp_number);)

Your assignment states that you need to use an input function, and output function. Your logic seems to all be there, you just need to change it into a function. Basically, your function will look something like this...
Code:
void getInput (void)
{
 for(i=0;i<10;i++){ 

  printf("Enter employee number: "); 
  scanf("%s", list[i].emp_number); 

  printf("Enter name: "); 
  scanf("%s", list[i].name); 

  printf("Enter hours worked: "); 
  scanf("%d", list[i].hours); 

  printf("Enter hourly rate: "); 
  scanf("%d", list[i].rate); 
 }
 return; 
}
No magic there, just copy n' pasting (see how I suggest changing emp_number to a number above).

Really, this assignment looks like the kinda thing you can compartmentalize entirely into functions. Here's what my main would look like...
Code:
int main ()
{
 // get 10 employee records from screen.
 getInput();

 // display gross pay calculated from hours worked and pay rate 
 displayPayHoursRate();

 // Displays totals for hours worked and gross pay; and
 //  averages for hours worked, pay rate, and gross pay
 displayTotalsAverage();

 // Sorts the records in ascending employee name order
 sortAscendingName();

 // redisplaying the employee info proves you sorted it.
 displayPayHoursRate();

 // exit program
 return 0;
}
All the work for this program should be hidden in those functions.
__________________
Callou collei we'll code the way
Of prime numbers and pings!

Last edited by QuestionC; 01-20-2002 at 12:00 AM.
QuestionC is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
returning char arrays!!!! bobthebullet990 C Programming 2 03-30-2006 07:05 AM
code condensing bcianfrocca C++ Programming 4 09-07-2005 09:22 AM
Storing strings in 2d char arrays problem rainmanddw C++ Programming 5 10-22-2003 05:41 PM
malloc with arrays of strings Lib C Programming 2 08-03-2003 10:46 PM
arrays of char strings Unregistered C Programming 2 10-24-2001 04:46 PM


All times are GMT -6. The time now is 06:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22