【问题标题】:Declaring a Array of Struct using Variables in C++在 C++ 中使用变量声明结构数组
【发布时间】:2020-12-04 18:11:46
【问题描述】:

尝试通过键入请求的限制来更改结构数组。当我尝试通过使数组声明成为变量来做到这一点时,它只是说“分段错误(核心转储)”我该如何解决这个问题并允许数组工作? 这是我正在尝试制作的代码示例:

#include <stdio.h>
struct Employee {
    char firstName[20];
    char lastName[20];
    unsigned int age;
    char gender;
    double hourlySalary;
};
int main(){
    int limit;
    printf("How much employees do you wish to input\n");
    scanf("%d", &limit);
    int number = limit;
    struct Employee Employ[number];
    
    for(unsigned int i = 0; i < limit; ++i){
        ("\n\t Enter employee first name\n");
        scanf("%s", &Employ[i].firstName);
        ("\n\t Enter employee last name\n");
        scanf("%s", &Employ[i].lastName);
        ("\n\t Enter employee age\n");
        scanf("%d", &Employ[i].age);
        ("\n\t Enter employee hourly salary\n");
        scanf("%lf", &Employ[i].hourlySalary);
    }
    for(unsigned int i = 0; i < limit; i++){
        printf("\n\tfirstname\tlastname\tage\thourlysalary\n%s\t%s\t%d\t%lf",Employ[i].firstName,Employ[i].lastName,Employ[i].age,Employ[i].gender,Employ[i].hourlySalary);
    }
}

预期结果和输入:

【问题讨论】:

  • 发布您正在运行的实际代码。由于"s 太多,无法编译。
  • 你确定你在写 C++ 吗?这在我看来像 C。
  • 此代码有一些与您的问题无关的拼写错误。请修复它们
  • 您似乎使用了一些教授 C 的学习材料。请注意,C 和 C++ 是两种不同的语言。如果你想学习 C++,那么你需要一些教授 C++ 的学习材料。这里是书籍合集:stackoverflow.com/questions/388242/…
  • C++ 编译器可以处理大部分但不是全部的 C 代码。最大的区别在于你如何解决问题。 C 和 C++ 存在很大的意识形态差异,因此在使用方式上也存在很大差异。他们应该如何被教导也有很大不同。太多的人从学校毕业后认为他们学过 C++,而实际上他们没有,这对他们在工业界造成了伤害。

标签: arrays c struct


【解决方案1】:

我使用在线 CPP 编译器来测试代码 - 即使使用古老的 cpp98 标准进行编译,我也无法使其运行。当使用其他人指出的 C 构造时。

但是我使用 c++11 标准并使用了流和向量 - 我做了一个工作得很好的例子。

#include <iostream>
#include <string>
#include <vector>

struct Employee 
{
    char firstName[20];
    char lastName[20];
    unsigned int age;
    char gender;
    double hourlySalary;
};

int main()
{
    int limit = 0;

    std::cout << "How many employees?\n";
    std::cin >> limit;

    //Employee Employ[size];
    std::vector<Employee> employees;

    for(int i = 0; i < limit; ++i)
    {
        employees.push_back(Employee());
        auto& employee = employees[i];
    
        std::cout << "first name?\n";
        std::cin >> employee.firstName;
    
        std::cout << "last name?\n";
        std::cin >> employee.lastName;
    
        int temp = 0;
        std::cout << "age?\n";
        std::cin >> temp; 
        employee.age = temp;
    
        std::cout << "gender?\n";
        std::cin >> employee.gender;
    
        std::cout << "salary?\n";
        std::cin >> employee.hourlySalary;
    }

    std::cout << "\n======================================================= \n";
    for(auto it = employees.begin(); it != employees.end(); ++it)
    {
        std::cout << (*it).firstName << "\t";
        std::cout << (*it).lastName << "\t";
        std::cout << (*it).age << "\t";
        std::cout << (*it).gender << "\t";
        std::cout << (*it).hourlySalary << "\n";
    }
}

这确实有效.. 但是有一些代码闻起来像是将原始字符串读取到大小受限的变量中。 (就像评论建议的大小限制变量是 20 个字符的 char 数组)

【讨论】:

  • 建议:全力以赴,将char数组替换为std::string
【解决方案2】:

你应该扫描一个不带&的char数组,数组的名字已经是一个指针了。

scanf("%s", Employ[i].firstName)

【讨论】:

    【解决方案3】:

    设法自己最终让它工作,可能是 & 就像上面所说的帖子

    #include<stdio.h>
    #include<string.h>
    
    struct employee
    {
        char firstName[20];
        char lastName[20];
        unsigned int age;
    
        double hourlySalary;
    };
    
    int main()
    {
        int i;
        int limit;
        printf("insert number of employees to enter\n");
        scanf("%d", &limit);
        struct employee arr_employ[limit];
        
    
        for(i = 0; i < limit;i++)
        {
            
    
            printf("Enter name: ");
            scanf("%s", arr_employ[i].firstName);
    
            printf("Enter last name: ");
            scanf("%s", arr_employ[i].lastName);
    
            printf("Enter age:");
            scanf("%u", &arr_employ[i].age);
            
            printf("Enter hourly Salary:");
            scanf("%lf",&arr_employ[i].hourlySalary);
        }
    
        printf("\n");
    
        for(i = 0; i < limit; i++)
        {
            printf("%s\t%s\t%u\t%.2f\n",arr_employ[i].firstName,arr_employ[i].lastName,arr_employ[i].age,arr_employ[i].hourlySalary);
        }
    
        // signal to operating system program ran fine
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 1970-01-01
      • 2021-12-17
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 2016-05-09
      • 2016-02-19
      相关资源
      最近更新 更多