-4

I am trying to link my student_info structure with my read_name function but I am having issues getting it to work properly and it won't compile. The errors I am getting now are error: ‘first_name’ was not declared in this scope and error: ‘last_name’ was not declared in this scope. I declared them in the structure however.

Here's my code:

#include <iostream>
using namespace std;

//Place your structure here for Step #1:

struct student_info 
{
  char first_name[15];
  char last_name[15];
  char crn[15];
  char course_designator[15];
  int section;
};


//Place any prototypes that use the structure here:

void read_name(student_info & first_name[], student_info & last_name[])
{
  cout << "enter first name" << endl;
  cin.getline(first_name, 15, '\n'); 
  cout << "enter last name" << endl;
  cin.getline(last_name, 15, '\n');
  first[0] = toupper(first_name[0]);
  last[0] = toupper(last_name[0]); 
  cout << "your name is " << first_name << " " <<  last_name << endl;
}

int main()    
{
  //For Step #2, create a variable of the struct here:

  student_info student;

  read_name(first_name, last_name);

  return 0;
}
cf-
  • 8,598
  • 9
  • 36
  • 58

1 Answers1

1

Things you can do to fix your problem.

  1. Change read_name to take a reference to a student_info.

    void read_name(student_info & student)
    
  2. Change the implementation of read_name to read the data into the first_name and last_name members of info.

    void read_name(student_info & student)
    {
        cout << "enter first name" << endl;
        cin.getline(student.first_name, 15, '\n'); 
        cout << "enter last name" << endl;
        cin.getline(student.last_name, 15, '\n');
        first[0] = toupper(student.first_name[0]);
        last[0] = toupper(student.last_name[0]); 
        cout << "your name is " << student.first_name << " "
             <<  student.last_name << endl;
    }
    
  3. From main, call read_name using student as argument.

    int main()    
    {
        //For Step #2, create a variable of the struct here:
    
        student_info student;
    
        read_name(student);
    
        return 0;
    }
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270