0

Trying to make it to where a struct array can be changed by typing out the requested limit. When I try to do this by making the array declaration a variable, it just says "Segmentation Fault(core dumped)"How can I fix this and allow the array to work? This is an example of the code I am trying to make:

#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);
    }
}

Expected results and input:

enter image description here

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    Post the actual code you are running. This doesn't compile because of one too many `"`s. – underscore_d Dec 04 '20 at 18:13
  • 4
    Are you sure you're writing C++? This looks like C to me. – cigien Dec 04 '20 at 18:14
  • 2
    this code has some typos not related to your question. Please fix them – 463035818_is_not_an_ai Dec 04 '20 at 18:16
  • or rather show the real code, the one that causes the segfault. See also [mcve] – 463035818_is_not_an_ai Dec 04 '20 at 18:17
  • also include the input in the question (best would be also output and expected output) – 463035818_is_not_an_ai Dec 04 '20 at 18:18
  • no segfault here: https://godbolt.org/z/dE5hba What is the input? Note that this code can be easily crashed with the "right" input. What input causes your segfault only you can know. – 463035818_is_not_an_ai Dec 04 '20 at 18:21
  • is this C perhaps? Or why are you avoiding to use any C++ in the code? – 463035818_is_not_an_ai Dec 04 '20 at 18:22
  • Used this https://www.onlinegdb.com/online_c++_compiler# and the inputs I tried were 2 and 3 for defining the number of arrays of struct, also, this is C, at least as far as I know, was just going by what is taught/ used for my class – Dragon Lord Dec 04 '20 at 18:23
  • no matter what compiler you use, if the user has a name longer than 20 characters then all bets are off. What is the input that causes the segfault? – 463035818_is_not_an_ai Dec 04 '20 at 18:24
  • use `std::vector` for dynamic arrays and `std::string` for strings and most of your problems will be gone – 463035818_is_not_an_ai Dec 04 '20 at 18:25
  • 1
    you seem to use some learning material that teaches C. Note that C and C++ are two different languages. If you want to learn C++ then you need some learning material that teaches C++. Here is a collection of books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Dec 04 '20 at 18:27
  • For a class, so not sure if I have the luxury of using that, nor do I know how to. Using the "C++" section of the dev c++ application I used so thought it was c++, if it is C I have changed it in the tabs, just want to get it to work with what I have right now... – Dragon Lord Dec 04 '20 at 18:32
  • 1
    A C++ compiler can process most, but not all, C code. The big difference is in how you solve the problem. C and C++ have large ideological differences and thus large differences in how they should be used. How they should be taught is also very different. Too many people come out of schools thinking they learned C++ when they didn't, and this hurts them in industry. – user4581301 Dec 04 '20 at 18:46
  • If your class purports to teach C programming, you need to a C compiler, rather than a C++ compiler, and tag your questions with the C tag. If it purports to teach C++ programming, try to be as far from this class as possible. – n. m. could be an AI Dec 04 '20 at 18:47

3 Answers3

0

I used an online CPP compiler to test the code - i was unable to make it run even when using ancient cpp98 standards for compiling. when using as others pointed out the C constructs.

However i did it with c++11 standards and used streams and vectors - and i made an example that worked just fine.

#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";
    }
}

This does work .. however there are some code smells like reading strings raw into size restricted variables. (like the comment suggests the size restricted variables are the char arrays of 20 characters)

0

You should scan a char array without the &, the name of the array is already a pointer.

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

Managed to finally get it to work myself, probably was the & like the post above stated

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