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

  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.

  7. #7
    Join Date
    Nov 2008
    Posts
    7
    Thanks, jonnin. I'm new to this, so your opinion is appreciated.

  8. #8
    Join Date
    Nov 2008
    Posts
    7
    Ran into a bit of a problem, I discovered the specifications states to use a void function. I deviated from using a void because I had trouble getting it to work, and a return function seemed like it made more sense. Bummer, but maybe it's a good thing...because this is probably something I need to know.

    So, my code is compiling, but the input data doesn't seem to be...going in, e.g., when testing the application, I'm prompted to input, but nothing is displayed in the output function. In addition, my output function, printRecords(), works fine before putting the input into a function.

    I tried using a parameter list and function for input like so:

    Code:
    void getRecords(Student& x)
    {
      cout << "Name: ";
      getline(cin, x.name);
    
      cout << "Address: ";
      getline(cin, x.address);
    
      ...
    }
    ...and in main...

    Code:
    for (i = 0; i < SIZE; i++)
    {
      cout << endl;
      cout << "//Student " << i + 1 << "/3" << endl;
      Student getRecParam = {a[i].name, a[i].address, a[i].city, a[i].zip, a[i].gender, a[i].id, a[i].gpa};
      getRecords(getRecParam);
    }
    I based the above code on how I did my input function. Here is the output function using a parameter list:

    Code:
    void printRecords(Student& t)
    {
      cout << endl;
      cout << "#Name: " << t.name << endl;
      cout << "#Address: " << t.address << endl;
      ...
      ...
    }
    ...and in main...

    Code:
    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);
    }
    Again, any help is appreciated!

    For reference, here is my complete program 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
    
    void 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);
    }
    
    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 recParam = {a[i].name, a[i].address, a[i].city, a[i].zip, a[i].gender, a[i].id, a[i].gpa};
        getRecords(recParam);
      }
    
      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;
    }
    Last edited by joel++; 11-21-2008 at 05:55 PM. Reason: typo in my getRecords function

  9. #9
    Join Date
    Dec 2007
    Posts
    401
    you want to get the information into the Student in the array, not a temporary variable.

    Code:
    for (i = 0; i < SIZE; i++)
      {
        cout << endl;
        cout << "//Student " << i + 1 << "/3" << endl;
        //Student recParam = {a[i].name, a[i].address, a[i].city, a[i].zip, a[i].gender, a[i].id, a[i].gpa};
        getRecords( a[i] );
      }

  10. #10
    Join Date
    Nov 2008
    Posts
    7
    How beautiful is that? Vijayan, thank you so much!

    I just want to reiterate how I see this taking place; please, anyone feel free to correct or fine tune me.

    In the loop, we are setting up the input from the function into, a[i], by placing it in the function call, getRecords. In the getRecords function, Student& x is used to collect and send the data to a[i].

    loop --> getRecords call --> & sets up a[i]
    getRecords function --> creates Student& x --> & sends parameters to a[i]

    Thanks again!

  11. #11
    Join Date
    Dec 2007
    Posts
    401
    > ... we are setting up the input from the function into, a[i], by placing it in the function call, getRecords.
    right.

    > In the getRecords function, Student& x is used to collect and send the data to a[i].
    x is simply an alias for (reference to) a[i]. (the data is directly entered into a[i]).

  12. #12
    Join Date
    Nov 2008
    Posts
    7
    Got it; thanks!

Similar Threads

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