Help with structure c code

I'm a beginner and I need help with a C code to define a structure. I understand some of the basic concepts but I just have trouble conceptualizing and putting the code together.

Write C Code to define a structure for employee. Each employee will have an employee number, first name, last name, hourly rate and number of hours worked.

#include <stdio.h>
struct Employee
{
int employeenumber[50];
string firstname;
string lastname;
float hourlyrate;
int hourswork;

} e;

int main()
{
printf("Enter information:\n");

printf("Enter employeenumber: ");
scanf("%d", e.employeenumber);

printf("Enter firstname: ");
scanf("%d", &e.firstname);

printf("Enter lastname: ");
scanf("%f", &e.lastname);

printf("Enter hourly rate: ");
scanf("%f", &e.hourlyrate);

printf("Enter hourswork: ");
scanf("%d", &e.hourswork);


printf("Displaying Information:\n");

printf("e: ");
puts(e.employeenumber);

printf("First Name: \n",e.firstame);

printf("Last Name: \n", e.lastname);

printf("hourly rate: %.1f", e.hourlyrate);

printf("hours work: ", e.hourswork);

return 0;
}
I got these error when I run it:

||=== Build: Debug in Question9 (compiler: GNU GCC Compiler) ===|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|5|error: 'string' does not name a type|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|6|error: 'string' does not name a type|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp||In function 'int main()':|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|20|error: 'struct Employee' has no member named 'firstname'|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|23|error: 'struct Employee' has no member named 'lastname'|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|35|error: cannot convert 'int*' to 'const char*' for argument '1' to 'int puts(const char*)'|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|37|error: 'struct Employee' has no member named 'firstame'|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|39|error: 'struct Employee' has no member named 'lastname'|
C:\Users\shanike mc killup\Desktop\Important Documents\Dev-C++ Programs\Question9\main.cpp|43|warning: too many arguments for format [-Wformat-extra-args]|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
Are you using c or c++? In c strings are most often represented as character arrays. In c++ you need to add #include <string> and access it through the std:: namespace (std::string firstname).
Why is employeenumber an array?
In C you can do it like this:
1
2
3
4
5
6
7
8
9
10
#define NAME_LEN  20

struct Employee
{
int employeenumber;
char firstname[NAME_LEN];
char lastname[NAME_LEN];
float hourlyrate;
int hourswork;
} e


Why do you try to read firstname and lastname as a number ?
1
2
3
4
5
printf("Enter firstname: ");
scanf("%d", &e.firstname);

printf("Enter lastname: ");
scanf("%f", &e.lastname);

Maybe have a look at your textbook or some reference about scanf format flags
Last edited on
Also, you can't use printf or scanf with C++ strings.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
he is using c. Its gcc not g++ and the assignment text says 'in C'.

I believe one of the problems is confusion that %s is needed for the 'strings'; %d and %f will both make a mess.

also, string is not a thing. use char[maxlength]; where maxlength is appropriate to your problem, probably a #define constant at the top of the program.

finally, my C is a bit rusty (thought I tend to write Cish c++ at times) ... in C specifically you MAY need to typdef structs to make them a type, otherwise you just get 1 instance of the thing or whatever you tagged onto the last brace when it is defined (?)
Last edited on
Topic archived. No new replies allowed.