newbie with compiler/syntax errors

Posted on 2006-04-02
Medium Priority
Last Modified: 2010-04-01
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;

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.
Question by:bnblazer
  • 2
LVL 23

Accepted Solution

brettmjohnson earned 1000 total points
ID: 16356981
The compiler tells you which line the error occurs on.  The actual problem is likely on that line (or the ones immediately preceding it).

In your case, employee is an array.  You need to reference a specific array element.

cin >> employee[i].id;

LVL 23

Expert Comment

ID: 16357003
Your array allocation is also wrong.  You can create a static array if you know the number of elements at compile time:

employee record[100];

However, if you don't know the size in advance, you will have to dynamically allocate the array after you do know the size:

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

employee * record = new employee[numEmp];


Author Comment

ID: 16357321
Thank you for your help.  I had to change one thing from what you wrote, but you got me on the right path.

cin >> record[i].id;

Thank you again,

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question