My last name won't display

The code is suppose to gather phone numbers using structures. I don't know what to do because the string won't output a last name.

Example:
Use 1 student then enter Mary Smith

Smith won't come up just Mary


------------------------

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>

using namespace std;
int Number_Of_Students;

struct Phone{
    int area_code;
    int prefix;
    int suffix;
    };

struct StudentPhoneRecord{
    string first_name, last_name;
    Phone home;
    Phone emergency;
    Phone peronal_doctor;
};

StudentPhoneRecord student [50];

void Set_Students()
{
    cout << "Number of students (must be a positive number less than or equal to 50) = ? ";
    cin >> Number_Of_Students;
    while (Number_Of_Students > 51)
        {cout << "50 Students or less! Please try again"<<endl;
        cin >>Number_Of_Students;}
}

void Gather_Numbers()
{
     for(int x = 1; x <= Number_Of_Students; x++)
    {
    cout << "Please enter the information for student "<< x<<"."<<endl;
    cout << endl;
    cout <<"Name: ";
    cin >> student[x].name; cin.ignore(1000, '\n');
    }
}

void Display_Data()
{
    for(int x = 1; x <= Number_Of_Students; x++)
    {
    cout << student[x].name << endl;
    }
}

int main() {
    Set_Students();
    Gather_Numbers();
    Display_Data();
}
Last edited on
PLEASE learn to use code tags, they will make reading and commenting on your code MUCH easier.

Hint: you can edit your post and add them.

http://www.cplusplus.com/articles/jEywvCM9/

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>

using namespace std;
int Number_Of_Students;

struct Phone
{
   int area_code;
   int prefix;
   int suffix;
};

struct StudentPhoneRecord
{
   string first_name, last_name;
   Phone home;
   Phone emergency;
   Phone peronal_doctor;
};

StudentPhoneRecord student[50];

void Set_Students()
{
   cout << "Number of students (must be a positive number less than or equal to 50) = ? ";
   cin >> Number_Of_Students;
   while (Number_Of_Students > 51)
   {
      cout << "50 Students or less! Please try again" << endl;
      cin >> Number_Of_Students;
   }
}

void Gather_Numbers()
{
   for (int x = 1; x <= Number_Of_Students; x++)
   {
      cout << "Please enter the information for student " << x << "." << endl;
      cout << endl;
      cout << "Name: ";
      cin >> student[x].name; cin.ignore(1000, '\n');
   }
}

void Display_Data()
{
   for (int x = 1; x <= Number_Of_Students; x++)
   {
      cout << student[x].name << endl;
   }
}

int main()
{
   Set_Students();
   Gather_Numbers();
   Display_Data();
}

Lines 17-23 you define your StudentPhoneRecord with TWO strings.

In your Gather_Numbers() and Display_Data() functions you access the student's name incorrectly. student[x].name.

It should be student[x].first_name & student[x].last_name.

I can't understand how this even compiles. There is no string called name in the struct.

A bit of a tweak to the two functions:
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
void Gather_Numbers()
{
   for (int x = 1; x <= Number_Of_Students; x++)
   {
      std::cout << "Please enter the information for student " << x << ".\n";

      std::cout << "First Name: ";
      std::cin >> student[x].first_name;
      
      std::cout << "Last Name: ";
      std::cin >> student[x].last_name;

      std::cout << '\n';
      // std::cin.ignore(1000, '\n'); // not needed
   }
}

void Display_Data()
{
   for (int x = 1; x <= Number_Of_Students; x++)
   {
      std::cout << student[x].last_name << ", " << student[x].first_name << '\n';
   }
}


And now you get both first and last name to output:
Number of students (must be a positive number less than or equal to 50) = ? 2
Please enter the information for student 1.
First Name: Henry
Last Name: Tubbett

Please enter the information for student 2.
First Name: Joe
Last Name: d'Ragman

Tubbett, Henry
d'Ragman, Joe
Thank you for showing me how to format and your help
use code tags

Little subject drift, what is described it http://www.cplusplus.com/articles/jEywvCM9/
does not work for me when starting a new topic. All buttons below the edit window, including the length counter, do nothing. I have to submit first and edit to do the mark-up. I use Firefox as browser.
Topic archived. No new replies allowed.