newbie with compiler/syntax errors

Posted on 2006-04-02
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 >>;
            cout << "Employee Last Name: ";
            cin >>;
            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
    LVL 23

    Accepted Solution

    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

    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];

    LVL 1

    Author Comment

    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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now