Newbie with an error

B

bnblazer

Please be gentle. I am new to C++ and trying to get an understanding
of it by working through some tutorials. The one I am working on now
has to do with "structs." Essentially, I need to build a struct that
will take some employee data and then enter it into an array. I think
I have the basic concept down, but I am getting some compiler errors
that I have never seen before, and am having a hard time figuring them
out. I am sure that it just some stupid typos, but I have been looking
at this too long and think that I can not see them. You help in
figuring this out is sincerely appreciated.

My cin statements are getting the error: "Expecting primary expression
before '.'"

My array (at the end of the for) is getting the same error except is
says "...before ']'"

Here is a code snippet:

#include <iostream>
using namespace std;

int main (int argc, char * const argv[]) {

int i;
int numEmp;

struct employee {
int id;
char name[20];
float rate;
float hours;
};

employee record[numEmp];

cout << "\n**************************************************" <<
endl;
cout << "********** EMPLOYEE DATA RECORD PROGRAM **********" << endl;
cout << "**************************************************" << endl;
cout << "This program will take employee records one at a time." <<
endl;
cout << "You will be prompted for the number of employees you want to
enter" << endl;
cout << "You will then enter ID#, Name, Rate, and Hours for each
employee" << endl;
cout << "When you are done, a chart will print to show you all data
entered." << endl << endl;


cout << "Please enter the number of employees you want to enter: ";
cin >> numEmp;

for (i=0; i < numEmp; i++)
{
cout << "Employee Number: ";
cin >> employee.id;
cout << "Employee Last Name: ";
cin >> employee.name;
cout << "Employee Rate: ";
cin >> employee.rate;
cout << "Employee Hours: ";
cin << employee.hours;
record[employee];
}
}

Again, thank you for your help. I have pegged my frustration meter,
and am feeling a bit embarrassed that I cant figure this one out
myself.

Brian
 
O

osmium

Please be gentle. I am new to C++ and trying to get an understanding
of it by working through some tutorials. The one I am working on now
has to do with "structs." Essentially, I need to build a struct that
will take some employee data and then enter it into an array. I think
I have the basic concept down, but I am getting some compiler errors
that I have never seen before, and am having a hard time figuring them
out. I am sure that it just some stupid typos, but I have been looking
at this too long and think that I can not see them. You help in
figuring this out is sincerely appreciated.

My cin statements are getting the error: "Expecting primary expression
before '.'"

My array (at the end of the for) is getting the same error except is
says "...before ']'"

Here is a code snippet:

#include <iostream>
using namespace std;

int main (int argc, char * const argv[]) {

int i;
int numEmp;

struct employee {
int id;
char name[20];
float rate;
float hours;
};

employee record[numEmp];

cout << "\n**************************************************" <<
endl;
cout << "********** EMPLOYEE DATA RECORD PROGRAM **********" << endl;
cout << "**************************************************" << endl;
cout << "This program will take employee records one at a time." <<
endl;
cout << "You will be prompted for the number of employees you want to
enter" << endl;
cout << "You will then enter ID#, Name, Rate, and Hours for each
employee" << endl;
cout << "When you are done, a chart will print to show you all data
entered." << endl << endl;


cout << "Please enter the number of employees you want to enter: ";
cin >> numEmp;

for (i=0; i < numEmp; i++)
{
cout << "Employee Number: ";
cin >> employee.id;

employee is a *type*. The syntax wants a variable, record in this case.
<snip>

The distinction between types and variables is sometimes pretty blurry.
Many people make the initial letter of a type a capital letter.

Thus:
struct Employee
{ ....
 
D

Daniel T.

Please be gentle. I am new to C++ and trying to get an understanding
of it by working through some tutorials. The one I am working on now
has to do with "structs." Essentially, I need to build a struct that
will take some employee data and then enter it into an array. I think
I have the basic concept down, but I am getting some compiler errors
that I have never seen before, and am having a hard time figuring them
out. I am sure that it just some stupid typos, but I have been looking
at this too long and think that I can not see them. You help in
figuring this out is sincerely appreciated.

My cin statements are getting the error: "Expecting primary expression
before '.'"

My array (at the end of the for) is getting the same error except is
says "...before ']'"

Here is a code snippet:

#include <iostream>
#include said:
using namespace std;

int main (int argc, char * const argv[]) {

I wouldn't bother with argc and argv unless they are actually being used
in the program.

int main() {
int i;
int numEmp;

Your variables weren't initialized. That's dangerous.
struct employee {
int id;
char name[20];

You should use a string in the above, as in:
string name;
float rate;
float hours;
};

employee record[numEmp];

how many employee objects will be made here? It's hard to say because
numEmp hasn't been given a value.
cout << "\n**************************************************" <<
endl;
cout << "********** EMPLOYEE DATA RECORD PROGRAM **********" << endl;
cout << "**************************************************" << endl;
cout << "This program will take employee records one at a time." <<
endl;
cout << "You will be prompted for the number of employees you want to
enter" << endl;
cout << "You will then enter ID#, Name, Rate, and Hours for each
employee" << endl;
cout << "When you are done, a chart will print to show you all data
entered." << endl << endl;


cout << "Please enter the number of employees you want to enter: ";
cin >> numEmp;

for (i=0; i < numEmp; i++)
{
cout << "Employee Number: ";
cin >> employee.id;
cout << "Employee Last Name: ";
cin >> employee.name;
cout << "Employee Rate: ";
cin >> employee.rate;
cout << "Employee Hours: ";
cin << employee.hours;
record[employee];

In the above, 'employee' is the name of your class, not the name of a
variable. Try changing to "record" instead.
}
}

Again, thank you for your help. I have pegged my frustration meter,
and am feeling a bit embarrassed that I cant figure this one out
myself.

Hope that helps.
 
J

Jaspreet

Please be gentle. I am new to C++ and trying to get an understanding
of it by working through some tutorials. The one I am working on now
has to do with "structs." Essentially, I need to build a struct that
will take some employee data and then enter it into an array. I think
I have the basic concept down, but I am getting some compiler errors
that I have never seen before, and am having a hard time figuring them
out. I am sure that it just some stupid typos, but I have been looking
at this too long and think that I can not see them. You help in
figuring this out is sincerely appreciated.

My cin statements are getting the error: "Expecting primary expression
before '.'"

My array (at the end of the for) is getting the same error except is
says "...before ']'"

Here is a code snippet:

#include <iostream>
using namespace std;

int main (int argc, char * const argv[]) {

int i;
int numEmp;

struct employee {
int id;
char name[20];
float rate;
float hours;
};
Not sure if this is a good practice of declaring a local struct.
Anyways this has nothing to do with your error.
employee record[numEmp];

What is numEmp ? Its a local variable with an un-intialised value. You
would need to take an input from the user and then declare this
statement. Basically, move :

cout << "Please enter the number of employees you want to enter: ";
cin >> numEmp;

before record variable declaration.

So, your statements would be:

cout << "Please enter the number of employees you want to enter: ";
cin >> numEmp;
employee record[numEmp];

Also, employee is a type and record is a variable. So you would need to
use record variable from here-after.
cout << "\n**************************************************" <<
endl;
cout << "********** EMPLOYEE DATA RECORD PROGRAM **********" << endl;
cout << "**************************************************" << endl;
cout << "This program will take employee records one at a time." <<
endl;
cout << "You will be prompted for the number of employees you want to
enter" << endl;
cout << "You will then enter ID#, Name, Rate, and Hours for each
employee" << endl;
cout << "When you are done, a chart will print to show you all data
entered." << endl << endl;


cout << "Please enter the number of employees you want to enter: ";
cin >> numEmp;
Moved above...
for (i=0; i < numEmp; i++)
{
cout << "Employee Number: ";
cin >> employee.id; Need to use record.
cout << "Employee Last Name: ";
cin >> employee.name; Need to use record.
cout << "Employee Rate: ";
cin >> employee.rate; Need to use record.
cout << "Employee Hours: ";
cin << employee.hours;

cin >> and not cin <<.
Need to use record.
record[employee];
//This statement makes no sense. Remove this statement.
}
}

Again, thank you for your help. I have pegged my frustration meter,
and am feeling a bit embarrassed that I cant figure this one out
myself.

No need to be embarrased. You need to make a start and the good thing
is you tried yourself before asking for help.
 
B

Brian

I want to take a minute to thank everyone who responded. I sincerely
appreciate the help. I was able to fix my errors.

Thank you very much,
Brian
 
M

markpark

hi : i'm not a c++ expert and i don't know what's wrong with the cin
statements.
but, i definitely am sure that you are going to run into other
problems because
you are defining the array size with a value,empnum, that is not known
by the compiler
until run time. if you want to do this sort of thing, you need to
dynamically
allocate using the "new" command. that's all i can tell you and it
really doesn't answer your question. good luck.
 
G

Gavin Deane

Please don't top-post. Thanks in advance.
hi : i'm not a c++ expert and i don't know what's wrong with the cin
statements.
but, i definitely am sure that you are going to run into other
problems because
you are defining the array size with a value,empnum, that is not known
by the compiler
until run time. if you want to do this sort of thing, you need to
dynamically
allocate using the "new" command. that's all i can tell you and it
really doesn't answer your question. good luck.

If you need to define an array with a size only known at runtime, use a
std::vector rather than allocating the memory by hand with new. new
should rarely appear outside a constructor and std::vector is designed
for exactly the job you describe.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top