c++从函数类型创建变量
C++ creating a variable from a function type
我正试图将我的student_info
结构与我的read_name
功能联系起来,但我有问题,让它正常工作,它不会编译。我现在得到的错误是error: ‘first_name’ was not declared in this scope
和error: ‘last_name’ was not declared in this scope
。但是,我在结构中声明了它们。
下面是我的代码:
#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;
}
你可以做些什么来解决你的问题
-
将
read_name
更改为对student_info
的引用void read_name(student_info & student)
-
修改
read_name
的实现,将数据读取到info
的first_name
和last_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; }
-
从
main
调用read_name
,使用student
作为参数。int main() { //For Step #2, create a variable of the struct here: student_info student; read_name(student); return 0; }
相关文章:
- 为什么在堆栈和堆上创建变量会产生相同的程序集代码?
- 如何在循环中动态创建变量(c++)
- 创建变量之间的运算符排列
- 是否有一种方法可以调用一个函数,而不会创建变量,而不会创建变量
- 在使用该名称创建变量后调用本机 C 函数
- C++ 仅当模板参数为 true 时才创建变量
- 创建变量实例有什么不同?
- 是否可以在不为该数组创建变量的情况下将数组作为参数传递到函数中?
- 为什么在默认构造函数中使用括号会导致创建变量?
- 动态创建变量名称的替代方法
- C++:使用void创建变量
- 如何在Arduino中从SD卡创建变量
- 应该如何使用C++11 Regex从存储在文本文件中的表中创建变量
- 从文本文件导入的不同线类型创建变量
- 创建变量的别名
- 避免在此模板代码中创建变量
- 类方法中创建变量的作用域
- 动态创建变量c++ /cli . net
- 使用来自文件的输入创建变量
- 创建变量名别名的方法