Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2003

    sigh....another C++ question

    It's been a good couple of weeks since I've been here to ask C++ questions, so I think I'm doing alright

    I'm trying to write a program that basically just sorts an array of structs by an employee id (a[index].id)

    Here is my current code...I think I'm having a problem with passing the arguments to my sorting function...

     Course number & title: COMS 2203 01 Foundations of Computer Programming II
     Semester:              Fall 2006
     Assignment number:     #2
     Author Name:           Kevin Cackler
     Date Written:          October 11, 2006
     Description of program:
     This program reads employee records from an input file, stores the records in
     an array of structs, sorts the records by employee id, and prints the sorted
     records (contents of the array) on the screen...
     The data of the input file should be in the following form:
     Employee ID
     Employee First Name
     Employee Last Name
     Employee Street Address
     Employee City
     Employee State (2 Letter Abbreviation)
     Employee Zip Code
     Employee Phone number
     For example:
     1200 West 64th Street
     (479) 555-6666
     Now on to the program...
    #include <iostream>
    #include <fstream>
    using namespace std;
    //Create the structs
    struct Name {
      string firstName;
      string lastName;
    struct Address {
      string street;
      string city;
      string state;
      string zip;
    struct Employee {
      string id;
      Name name;
      Address address;
      string phoneNumber;
    const int N = 100;  //Size of our array of structs
    void bubbleSort(Employee list, int length);  //Sorting function prototype
    int main()
      Employee a[N];  //Our array of structs
      string fname;  //Input file name
      ifstream infile;  //Input file
      int counter = 0;  //Used for couting during a while loop
      cout << "Please input the name of the data file: ";
      cin >> fname;;
      if(!infile) {  //Do some error checking to make sure we could open the file
        cerr << "Error:  Can't open data file" << endl;
        return 1;
      infile >> a[0].id;
      infile >> a[0].name.firstName;
      infile >> a[0].name.lastName;
      infile.ignore();  //Right here we take the newline character out of the input stream
      infile >> a[0];
      infile >> a[0].address.state;
      infile >> a[0];
      infile >> a[0].phoneNumber;
      counter ++;
      while(infile) {
        infile >> a[counter].id;
        infile >> a[counter].name.firstName;
        infile >> a[counter].name.lastName;
        infile >> a[counter];
        infile >> a[counter].address.state;
        infile >> a[counter];
        infile >> a[counter].phoneNumber;
        counter ++;
      //Call the bubbleSort function to sort the data...
      bubbleSort(a, N); 
      return 0;
    void bubbleSort(Employee list, int length)
      string temp;
      int counter, index;
      for (counter = 0; counter < length - 1; counter ++)
        for (index = 0; index < length - 1 - counter; index++)
          if (list[index].id > list[index + 1].id)
            temp = list[index].id;
            list[index].id = list[index + 1].id;
            list[index + 1].id = temp;
    When I try to compile, I get
    In function `int main()':
    program.cpp:113: error: conversion from `Employee*' to non-scalar type `
    Employee' requested
    program.cpp: In function `void bubbleSort(Employee, int)':
    program.cpp:127: error: no match for 'operator[]' in 'list[index]'
    program.cpp:127: error: no match for 'operator[]' in 'list[(index + 1)]'
    program.cpp:129: error: no match for 'operator[]' in 'list[index]'
    program.cpp:130: error: no match for 'operator[]' in 'list[index]'
    program.cpp:130: error: no match for 'operator[]' in 'list[(index + 1)]'
    program.cpp:131: error: no match for 'operator[]' in 'list[(index + 1)]'
    I just want to know how I'm supposed to pass the array of structs to the sorting function....
    I've been hacking away at this little problem for well over an hour, searching the internet, etc...and I've lost hope...Any help, as always, is very appreciated..

  2. #2
    Join Date
    Feb 2005
    It's been years since I programmed in C++, but try this.

    An array is a pointer, so you have to pass the array by its address using &.

    //Call the bubbleSort function to sort the data...
    bubbleSort(&a, N);


    if that doesn't work... try keeping bubbleSort(a,N) and changing the void function with []

    void bubbleSort(Employee list[], int length)
    Last edited by MrMan; 10-12-2006 at 12:20 AM.

  3. #3
    Join Date
    Feb 2003
    I feel like a complete tool....It was indeed that I wasn't declaring the function with the brackets...grrr..


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts