S
S
Sergo Zar2020-12-12 00:07:46
C++ / C#
Sergo Zar, 2020-12-12 00:07:46

Is the structure not being passed to the function?

you need to make a structure with kings and data about them. I'm doing something like this

#include <iostream>

using namespace std;

int l = 0;

struct knigga{
    string author;
    string name;
    string vid;
    int starostb;
    string misze;
    
};

void showinfo(knigga book, string src){
    for (int i = 0; i < l; i++){
        cout << book[i].name << endl;
    }
}

int main(){
    const int length = 8;
    l = length;
    knigga books[length] = {
        {"SergoZar", "Душевні рани", "Sergo.Zar.Production", 2021, "В майбутньому"},
        {"SergoZar", "Збірник V6", "Sergo.Zar.Production", 2019, "spaces.ru"},
        {"Олексій Семикопенко", "Підручник HTML", "Без видавництва", 2009, "webremeslo.ru"},
        {"Олексій Семикопенко", "Підручник CSS", "Без видавництва", 2009, "webremeslo.ru"},
        {"Джейсон Шреєр", "Кров піт і пікселі", "Форс", 2019, ""},
        {"Едріан Прутяну", "Як стати хакером", "ДМК Прес", 2020, ""},
        {"Джон Еріксон", "Хакінг. Мистецтво експлойту", "Пітер Прес", 2018, ""},
        {"Крейг Джеллі", "MINECRAFT Довідник архітектора", "Mojang", 2019, ""}
    };
    string src;
    cout << "Пошук: ";
    cin >> src;
    showinfo(books,src);
    
    return 0;
}

what am I doing wrong?:
9.cpp: In function 'void showinfo(knigga, std::__cxx11::string)':
9.cpp:18:21: error: no match for 'operator[]' (operand types are 'knigga' and 'int')
         cout << book[i].name << endl;
                     ^
9.cpp: In function 'int main()':
9.cpp:50:11: error: could not convert '(knigga*)(& books)' from 'knigga*' to 'knigga'
  showinfo(books,src);
           ^~~~~


and for some reason it works, although the logic is the same as in the previous code
#include <iostream>

using namespace std;
int l =0;
struct Employee
{
    short id;
    int age;
    double salary;
};
 
void printInformation(Employee *employee)
{
    for(int i = 0; i < l;i++){
    cout << "ID: " << employee[i].id << "\n";
    cout << "Age: " << employee[i].age << "\n";
    cout << "Salary: " << employee[i].salary << "\n";
    }
}
 
int main()
{
    const int  ll = 2;
    l = ll;
    Employee john[ll] = {{ 21, 27, 28.45 }, { 22, 29, 19.29 }};
 
    printInformation(john);
 
 
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2020-12-12
@Sergomen

void showinfo(knigga *book, string src){

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question