简体   繁体   English

创建结构并传递功能

[英]creating structure and passing it function

I need a help with proper creating function and then passint it to function. 我需要正确创建函数的帮助,然后将其传递给函数。 I had a piece of code like this: 我有一段这样的代码:

struct phones {
    char lastname[15];
    char number[8];
    char city[15];
} *list;

void enter(phones[], int);
int main()
{
    int n;
    cout << "Enter the number of users: ";
    cin >> n;
    list = new phones[n];
    enter(list, n);
    system("pause");
    delete[]list;
    return 0;
}
void enter(phones list[], int n) {
    for (int i = 0; i < n; i++)
    {
        cout << "User number " << i + 1 << endl;
        cout << "Enter user's lastname ";
        cin >> list[i].lastname;
        cout << "Enter user's number: ";
        cin >> list[i].number;
        cout << "Enter user's city: ";
        cin >> list[i].city;
    }
}

My teacher told me that it's not good to create a structure using *list right in it. 我的老师告诉我,在其中使用*list创建结构并不好。 Now i have a question how to create the structure properly, declare a size from user's input and then pass it to function. 现在我有一个问题,如何正确地创建结构,从用户输入中声明一个大小,然后将其传递给函数。

You can always define variables after declaring a struct : 您始终可以在声明struct后定义变量:

phones *list;

C++ doesn't have VLA so either use dynamic allocation, which is disallowsled according to your description, or use the special stuff in C++: STL. C ++没有VLA,所以要么使用动态分配(根据您的描述是不允许的),要么使用C ++中的特殊功能:STL。

Create a vector of struct phones can make it very simple: 创建struct phones的向量可以使其非常简单:

#include <vector>

// After input number
std::vector<phones> list(n);

And you won't have to bother with new s and delete s. 而且您不必费心newdelete

Just for fun, I rewrote your program, including a function to display the read data. 只是为了好玩,我重写了您的程序,其中包括一个显示读取数据的功能。 This is how I would have it: 这就是我的想法:

#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

struct User {
  char lastname[15];
  char number[8];
  char city[15];
};

void enter(User users[], int n)
{
  for (int i = 0; i < n; ++i) {
    cout << "[User number " << (i + 1) << "]" << endl;
    cout << "Enter user's lastname: ";
    cin >> users[i].lastname;
    cout << "Enter user's number: ";
    cin >> users[i].number;
    cout << "Enter user's city: ";
    cin >> users[i].city;
  }
}

void show(User users[], int n)
{
  for (int i = 0; i < n; ++i) {
    cout << "[User number " << (i + 1) << "]" << endl;
    cout << "Lastname: " << users[i].lastname << endl;
    cout << "Number: " << users[i].number << endl;
    cout << "City: " << users[i].city << endl;
  }
}

int main()
{
  int n;
  cout << "Enter the number of users: ";
  cin >> n;
  User* users = new User[n];
  enter(users, n);

  cout << "-- Displaying users: --" << endl;
  show(users, n);

  system("pause");
  delete[] users;
  return 0;
}

You could also eliminate the problem in your program by preceding a typedef before struct and using the List 您还可以通过在struct之前添加typedef并使用List来消除程序中的问题

example as follow: 示例如下:

typedef struct Person {
  char lastname[15];
  char number[8];
  char city[15];
} * List;

void enter(List list, int n) ;
void show(List list, int n);
int main()
{
  int n;
  cout << "Enter the number of users: ";
  cin >> n;
  List users = new Person[n];
  enter(users, n);
  cout << "-- Displaying users: --" << endl;
  show(users, n);
  system("pause");
  delete[] users;
  return 0;
}

void show(List list, int n)
{
  for (int i = 0; i < n; ++i) {
    cout << "[User number " << (i + 1) << "]" << endl;
    cout << "Lastname: " << list[i].lastname << endl;
    cout << "Number: " << list[i].number << endl;
    cout << "City: " << list[i].city << endl;
  }

}

void enter(List list, int n) {
    for (int i = 0; i < n; i++)
    {
        cout << "User number " << i + 1 << endl;
        cout << "Enter user's lastname ";
        cin >> list[i].lastname;
        cout << "Enter user's number: ";
        cin >> list[i].number;
        cout << "Enter user's city: ";
        cin >> list[i].city;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM