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