collections

So i am writing a program that needs to use a single record in the parameter list for the printStudent subprogram instead of the whole collection using a loop inside int main() for some reason it seems to compile and work but when i type a name with a first and last name the output only shows the first name what do i need to change to get the program to output both first and last name?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

struct Student
{
  string name;
  int id;
  char gender;
  float gpa;
}; // Student
 
void printStudents(deque<Student>& student)
{
  int i;
  cout << endl;
  cout << "Name = " << left << setw(20) << student[i].name;
  cout.fill('0'); 
  cout << " ID = " << right << setw(7) << student[i].id
       << ", gpa = " << student[i].gpa << endl;
  cout << "Gender = " << student[i].gender << endl;
  cout.fill(' '); 
  } // void 

int main()
{
  int n;
  
  cout << endl;
  cout << "How many records to be entered?: ";
  cin >> n;
  cin.ignore(1000, 10);

  int nRecords = 0;

  // create an empty list
  deque<Student> student;
 
  // read and save the records
  while (nRecords++ < n)
  {
    Student aStudent;
    cout << endl;
    cout << "Enter name: ";
    cin >> aStudent.name;
    cin.ignore(1000, 10);

    cout << "Enter ID number: ";
    cin >> aStudent.id;
    cin.ignore(1000, 10);
 
    cout << "Enter GPA: ";
    cin >> aStudent.gpa;
    cin.ignore(1000, 10);
 
    cout << "Enter Gender [M/F]: ";
    cin >> aStudent.gender;
    cin.ignore(1000, 10);
 
    student.push_back(aStudent);
    cout << endl;
    
  } // while
 
  cout << endl;
  int i;
  for (i = 0; i < student.size(); i++)
  {
    printStudents(student);
    cout << " " << " ";
  }
  cout << endl;
  return 0;
} // main 
Change cin >> name; to getline(cin, name);. Also, remove the cin.ignore after it.

Though this shouldn't work at all. The "i" on line 69 has nothing to do with "i" on line 18.
You should change the function to take a Student argument and pass student[i] to it (of course you'll need to change the function a bit).
Topic archived. No new replies allowed.