2014-05-14 32 views
-4

我想链接我的student_info结构与我的read_name函数,但我有问题让它正常工作,它不会编译。我现在得到的错误是error: ‘first_name’ was not declared in this scopeerror: ‘last_name’ was not declared in this scope。然而,我在结构中声明了它们。从函数类型创建变量的C++

这里是我的代码:

#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; 
} 
+6

什么错误...? – Qix

+0

错误:'first_name'声明为引用数组 25:43:错误:预计')'之前','令牌 25:58:错误:预期初始化之前'&'令牌 – user3226213

+1

'read_name'预计接收2参数,你没有参数调用它。 – Barmar

回答

1

事情可以做,以解决您的问题。

  1. 变化read_name采取一个student_info的引用。

    ​​
  2. 变化read_name的实施,将数据读入的infofirst_namelast_name成员。

    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. main,使用student作为参数调用read_name

    int main()  
    { 
        //For Step #2, create a variable of the struct here: 
    
        student_info student; 
    
        read_name(student); 
    
        return 0; 
    }