Array, Function, and Loop - In Sync, not the boy band


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Array, Function, and Loop - In Sync, not the boy band

Hybrid View

  1. #1
    Join Date
    Nov 2008
    Posts
    7

    Array, Function, and Loop - In Sync, not the boy band

    Hi DevX community,

    This is my first post!

    I'm a beginner programmer in C++ and I'm having trouble with integrating a struct array and loop with a function - by the way, this is a lab project for school. The objective is to create a small contact database for a class and includes the following:
    (1) create an array for 3 records
    (2) create a function using a loop to prompt user console input for the 3 records
    (3) create a function to output the three records

    I first developed the program with out functions, and it ran fine. I then was able to get the output into a function; however, I ran into trouble when putting the user input into a function.

    I understand array values may be initialized like so:
    Code:
    Student a = {"George Washington", 7065 lancaster ct, Dublin...};
    My idea, which isn't working, was to call a function from a loop, put the function input into a struct variable, x, and then return it to the array initialization in the loop. Here is the basic idea behind my failing trial:

    Code:
    struct arrayExample
    {
      string name;
      string address;
      int zip;
    }
    
    arrayExample getRecords(arrayExample& x)
    {
      cout << "Name: ";
      getline(cin, x.name);
    
      cout << "Address: ";
      getline(cin, x.address);
    
      cout << "Zip: ";
      cin >> x.zip;
      cin.ignore(1000, 10);
    
      return x;
    }
    
    int main()
    {
      const int SIZE = 3;
      Student a[SIZE];
      int i;
      
      cout << "==== Student Data Entery ====" << endl;
      for (i = 0; i < SIZE; i++)
      {
        cout << endl;
        cout << "//Student " << i + 1 << "/3" << endl;
        Student s = getRecords();
        a[i] = {s};
      }
    
      return 0;
    }
    Any help is greatly appreciated!

    Compiler: GNU through console on Mac OS X
    Errors given:
    students.cpp:16: error: too few arguments to function ‘Student getRecords(Student&)’
    students.cpp:69: error: at this point in file
    students.cpp:70: error: expected primary-expression before ‘{’ token
    students.cpp:70: error: expected `;' before ‘{’ token

    Here is the complete code:

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    struct Student
    {
      string name;
      string address;
      string city;
      int zip;
      char gender;
      int id;
      float gpa;
    }; // Student
    
    Student getRecords(Student& x)
    {
        cout << "Name: ";
        getline(cin, x.name);
    
        cout << "Address: ";
        getline(cin, x.address);
    
        cout << "City: ";
        getline(cin, x.city);
    
        cout << "Zip: ";
        cin >> x.zip;
        cin.ignore(1000, 10);
    
        cout << "Gender [m/f]: ";
        cin >> x.gender;
        cin.ignore(1000, 10);
    
        cout << "ID: ";
        cin >> x.id;
        cin.ignore(1000, 10);
    
        cout << "GPA: ";
        cin >> x.gpa;
        cin.ignore(1000, 10);
    
        return x;
    }
    
    void printRecords(Student& t)
    {
      cout << endl;
      cout << "#Name: " << t.name << endl;
      cout << "#Address: " << t.address << endl;
      cout << "#City: " << t.city << endl;
      cout << "#Zip: " << t.zip << endl;
      cout << "#Gender: " << t.gender << endl;
      cout << "#ID: " << t.id << endl;
      cout << "#GPA: " << t.gpa << endl;
    }
    
    int main()
    {
      const int SIZE = 3;
      Student a[SIZE];
      int i;
      
      cout << "==== Student Data Entery ====" << endl;
      for (i = 0; i < SIZE; i++)
      {
        cout << endl;
        cout << "//Student " << i + 1 << "/3" << endl;
        Student s = getRecords();
        a[i] = {s};
      }
    
      cout << endl;
      
      cout << "==== Student Records ====" << endl;
      for (i = 0; i < SIZE; i++)
      {
        Student outPrint = {a[i].name, a[i].address, a[i].city, a[i].zip, a[i].gender, a[i].id, a[i].gpa};
        printRecords(outPrint);
      }
      cout << endl;
    
      return 0;
    }

  2. #2
    Join Date
    Oct 2008
    Posts
    31
    Ok firstly your first mistake is here ...

    Student getRecords(Student &x)

    Because you are then using the function like "GetRecord()" but its expecting the struc to be put inside it. What you could do though is this ...

    Code:
    struct Student
    {
      string name;
      string address;
      string city;
      int zip;
      char gender;
      int id;
      float gpa;
    } x; // Student
    
    Student getRecords()
    {
    
        cout << "Name: ";
        getline(cin, x.name);
    
        cout << "Address: ";
        getline(cin, x.address);
    
        cout << "City: ";
        getline(cin, x.city);
    
        cout << "Zip: ";
        cin >> x.zip;
        cin.ignore(1000, 10);
    
        cout << "Gender [m/f]: ";
        cin >> x.gender;
        cin.ignore(1000, 10);
    
        cout << "ID: ";
        cin >> x.id;
        cin.ignore(1000, 10);
    
        cout << "GPA: ";
        cin >> x.gpa;
        cin.ignore(1000, 10);
    
        return x;
    }
    And your second program is let me check ...

    Haha!

    Here ...

    a[i] = {s};

    You don't put {s} you just put "s". Heres the full working code.

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    struct Student
    {
      string name;
      string address;
      string city;
      int zip;
      char gender;
      int id;
      float gpa;
    } x; // Student
    
    Student getRecords()
    {
    
        cout << "Name: ";
        getline(cin, x.name);
    
        cout << "Address: ";
        getline(cin, x.address);
    
        cout << "City: ";
        getline(cin, x.city);
    
        cout << "Zip: ";
        cin >> x.zip;
        cin.ignore(1000, 10);
    
        cout << "Gender [m/f]: ";
        cin >> x.gender;
        cin.ignore(1000, 10);
    
        cout << "ID: ";
        cin >> x.id;
        cin.ignore(1000, 10);
    
        cout << "GPA: ";
        cin >> x.gpa;
        cin.ignore(1000, 10);
    
        return x;
    }
    
    void printRecords(Student& t)
    {
      cout << endl;
      cout << "#Name: " << t.name << endl;
      cout << "#Address: " << t.address << endl;
      cout << "#City: " << t.city << endl;
      cout << "#Zip: " << t.zip << endl;
      cout << "#Gender: " << t.gender << endl;
      cout << "#ID: " << t.id << endl;
      cout << "#GPA: " << t.gpa << endl;
    }
    
    int main()
    {
      const int SIZE = 3;
      Student a[SIZE];
      int i;
    
      cout << "==== Student Data Entery ====" << endl;
      for (i = 0; i < SIZE; i++)
      {
        cout << endl;
        cout << "//Student " << i + 1 << "/3" << endl;
        Student s = getRecords();
        a[i] = s;
      }
    
      cout << endl;
    
      cout << "==== Student Records ====" << endl;
      for (i = 0; i < SIZE; i++)
      {
        Student outPrint = {a[i].name, a[i].address, a[i].city, a[i].zip, a[i].gender, a[i].id, a[i].gpa};
        printRecords(outPrint);
      }
      cout << endl;
    
      return 0;
    }
    - Nicky

  3. #3
    Join Date
    Nov 2008
    Posts
    7
    Nicky, thank you so much for the reply. Yes, it works!

    Though, I have several questions. I noticed you placed an x after the struct, i.e.:
    Code:
    struct Student
    {
      string name;
      string address;
      string city;
      int zip;
      char gender;
      int id;
      float gpa;
    } x; // Student
    This is something I haven't seen before; again, I'm new to this. I also noticed it works when declaring an array (is that the correct terminology?) within the getRecords function.

    Code:
    Student getRecords()
    {
      Student x;
    
      cout << "Name: ";
      getline(cin, x.name);
    
      cout << "Address: ";
      getline(cin, x.address);
      ...
      ...
    }
    I'm still trying to grasp this. I assume placing the x after the struct is another efficient way of creating an array?

    Thanks again!

  4. #4
    Join Date
    Oct 2008
    Posts
    31
    I'm not sure what you mean. Doing this ...

    Code:
    struct Student
    {
      string name;
      string address;
      string city;
      int zip;
      char gender;
      int id;
      float gpa;
    } x; // Student
    is the same as ...

    Code:
    struct Student
    {
      string name;
      string address;
      string city;
      int zip;
      char gender;
      int id;
      float gpa;
    }; // Student
    
    Student x;
    It just looks cleaner thats all. You can also do mutiple ones ...

    Code:
    struct Student
    {
    
      int zip;
      char gender;
      int id;
      float gpa;
    } x, y, b[1], etc; // Student
    Does that help?

  5. #5
    Join Date
    Nov 2008
    Posts
    7
    It certainly helps - thank you so much!

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    I will say that IMO it is cleaner to do

    student x;
    than to lump it onto the class/struct creation. There is a reason you rarely see

    class cl
    {
    ...
    } x,y,z;

    No one does this, but somehow its ok when its a struct (and, the difference between class and struct is small).


    I would stick to making your structs look like classes so the style is more consistent.

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2008, 09:27 AM
  2. String Array in C++
    By saruman in forum C++
    Replies: 13
    Last Post: 11-19-2007, 03:51 PM
  3. string array in c++
    By emeric in forum C++
    Replies: 11
    Last Post: 07-16-2007, 09:36 PM
  4. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 11:18 AM
  5. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 04:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center