C++ Printing array of struct objects












0















I have a problem with printing an array of struct objects.



Here is the code:



#include <iostream>
#include <string>

#define LOG(x) std::cout << x
#define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0])

struct vec2
{
int x, y;
};

struct Painting : vec2
{
std::string s_author, s_title, s_genre;
};

void disp_Array(Painting arr)
{
for (int i = 0; i < ARR_SIZE(arr); i++)
{
LOG(arr[i].s_author << "n");
LOG(arr[i].s_title << "n");
LOG(arr[i].s_genre << "n");
LOG(arr[i].x << "n");
LOG(arr[i].y << "n");
}
}

int main()
{
Painting painting[4];

disp_Array(painting);

for (int i = 0, j = 1; i < ARR_SIZE(painting), j <= ARR_SIZE(painting); i++, j++)
{
LOG("Painting " << j << "n");
LOG("Author: ");
std::cin.ignore();
std::getline(std::cin, painting[i].s_author);

LOG("Title: ");
std::getline(std::cin, painting[i].s_title);

LOG("Genre: ");
std::getline(std::cin, painting[i].s_genre);

LOG("Width: ");
std::cin >> painting[i].x;

LOG("Height: ");
std::cin >> painting[i].y;

LOG("n");
}

disp_Array(painting);

std::cin.get();
return 0;
}


The problem is within the disp_Array function. The idea was that the function should output the variables from objects which are in an array. But unfortunately it doesn't work, in fact, it doesn't output anything.



Therefore I tried to test it by replacing the function's body with just displaying the size of the array, but it prints 0 instead of 4 (the number of elements in that array).



Does that mean that the problem is within the function's parameters and how I pass them? Because on its own, ARRAY_SIZE works very well.










share|improve this question


















  • 3





    You can't copy C-style arrays, and thus can't pass them as parameters. Use std::array or std::vector, which both have a size() member function.

    – Quentin
    Jan 18 at 14:22






  • 4





    Your ARR_SIZE only work with real arrays. Once you pass the array to a function, it will decay to a pointer to its first element (as an argument Painting arr is equal to Painting* arr). And using sizeof on a pointer gives the size of the pointer and not what it points to. My advice is to stop using old simple plain C-style arrays, and instead use std::array or std::vector instead.

    – Some programmer dude
    Jan 18 at 14:23













  • Thank you. I had that feeling that I should do it how it should be done and ease my life, but still I gave it a try. Also there is something strange, when I enter the second element's variables it skips Author. I fixed that with cin.ignore() but I should then press enter, how can I avoid pressing enter and still have that problem fixed?

    – John Davis
    Jan 18 at 14:49











  • @JohnDavis: it would be a different question but lies in the mixing of getline (line oriented) and cin>> (field oriented). After reading a field followed by enter, the end of line is still in input buffer and causes the next getline to return an (almost) empty line. Long story short, dont mix.

    – Serge Ballesta
    Jan 18 at 14:54
















0















I have a problem with printing an array of struct objects.



Here is the code:



#include <iostream>
#include <string>

#define LOG(x) std::cout << x
#define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0])

struct vec2
{
int x, y;
};

struct Painting : vec2
{
std::string s_author, s_title, s_genre;
};

void disp_Array(Painting arr)
{
for (int i = 0; i < ARR_SIZE(arr); i++)
{
LOG(arr[i].s_author << "n");
LOG(arr[i].s_title << "n");
LOG(arr[i].s_genre << "n");
LOG(arr[i].x << "n");
LOG(arr[i].y << "n");
}
}

int main()
{
Painting painting[4];

disp_Array(painting);

for (int i = 0, j = 1; i < ARR_SIZE(painting), j <= ARR_SIZE(painting); i++, j++)
{
LOG("Painting " << j << "n");
LOG("Author: ");
std::cin.ignore();
std::getline(std::cin, painting[i].s_author);

LOG("Title: ");
std::getline(std::cin, painting[i].s_title);

LOG("Genre: ");
std::getline(std::cin, painting[i].s_genre);

LOG("Width: ");
std::cin >> painting[i].x;

LOG("Height: ");
std::cin >> painting[i].y;

LOG("n");
}

disp_Array(painting);

std::cin.get();
return 0;
}


The problem is within the disp_Array function. The idea was that the function should output the variables from objects which are in an array. But unfortunately it doesn't work, in fact, it doesn't output anything.



Therefore I tried to test it by replacing the function's body with just displaying the size of the array, but it prints 0 instead of 4 (the number of elements in that array).



Does that mean that the problem is within the function's parameters and how I pass them? Because on its own, ARRAY_SIZE works very well.










share|improve this question


















  • 3





    You can't copy C-style arrays, and thus can't pass them as parameters. Use std::array or std::vector, which both have a size() member function.

    – Quentin
    Jan 18 at 14:22






  • 4





    Your ARR_SIZE only work with real arrays. Once you pass the array to a function, it will decay to a pointer to its first element (as an argument Painting arr is equal to Painting* arr). And using sizeof on a pointer gives the size of the pointer and not what it points to. My advice is to stop using old simple plain C-style arrays, and instead use std::array or std::vector instead.

    – Some programmer dude
    Jan 18 at 14:23













  • Thank you. I had that feeling that I should do it how it should be done and ease my life, but still I gave it a try. Also there is something strange, when I enter the second element's variables it skips Author. I fixed that with cin.ignore() but I should then press enter, how can I avoid pressing enter and still have that problem fixed?

    – John Davis
    Jan 18 at 14:49











  • @JohnDavis: it would be a different question but lies in the mixing of getline (line oriented) and cin>> (field oriented). After reading a field followed by enter, the end of line is still in input buffer and causes the next getline to return an (almost) empty line. Long story short, dont mix.

    – Serge Ballesta
    Jan 18 at 14:54














0












0








0








I have a problem with printing an array of struct objects.



Here is the code:



#include <iostream>
#include <string>

#define LOG(x) std::cout << x
#define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0])

struct vec2
{
int x, y;
};

struct Painting : vec2
{
std::string s_author, s_title, s_genre;
};

void disp_Array(Painting arr)
{
for (int i = 0; i < ARR_SIZE(arr); i++)
{
LOG(arr[i].s_author << "n");
LOG(arr[i].s_title << "n");
LOG(arr[i].s_genre << "n");
LOG(arr[i].x << "n");
LOG(arr[i].y << "n");
}
}

int main()
{
Painting painting[4];

disp_Array(painting);

for (int i = 0, j = 1; i < ARR_SIZE(painting), j <= ARR_SIZE(painting); i++, j++)
{
LOG("Painting " << j << "n");
LOG("Author: ");
std::cin.ignore();
std::getline(std::cin, painting[i].s_author);

LOG("Title: ");
std::getline(std::cin, painting[i].s_title);

LOG("Genre: ");
std::getline(std::cin, painting[i].s_genre);

LOG("Width: ");
std::cin >> painting[i].x;

LOG("Height: ");
std::cin >> painting[i].y;

LOG("n");
}

disp_Array(painting);

std::cin.get();
return 0;
}


The problem is within the disp_Array function. The idea was that the function should output the variables from objects which are in an array. But unfortunately it doesn't work, in fact, it doesn't output anything.



Therefore I tried to test it by replacing the function's body with just displaying the size of the array, but it prints 0 instead of 4 (the number of elements in that array).



Does that mean that the problem is within the function's parameters and how I pass them? Because on its own, ARRAY_SIZE works very well.










share|improve this question














I have a problem with printing an array of struct objects.



Here is the code:



#include <iostream>
#include <string>

#define LOG(x) std::cout << x
#define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0])

struct vec2
{
int x, y;
};

struct Painting : vec2
{
std::string s_author, s_title, s_genre;
};

void disp_Array(Painting arr)
{
for (int i = 0; i < ARR_SIZE(arr); i++)
{
LOG(arr[i].s_author << "n");
LOG(arr[i].s_title << "n");
LOG(arr[i].s_genre << "n");
LOG(arr[i].x << "n");
LOG(arr[i].y << "n");
}
}

int main()
{
Painting painting[4];

disp_Array(painting);

for (int i = 0, j = 1; i < ARR_SIZE(painting), j <= ARR_SIZE(painting); i++, j++)
{
LOG("Painting " << j << "n");
LOG("Author: ");
std::cin.ignore();
std::getline(std::cin, painting[i].s_author);

LOG("Title: ");
std::getline(std::cin, painting[i].s_title);

LOG("Genre: ");
std::getline(std::cin, painting[i].s_genre);

LOG("Width: ");
std::cin >> painting[i].x;

LOG("Height: ");
std::cin >> painting[i].y;

LOG("n");
}

disp_Array(painting);

std::cin.get();
return 0;
}


The problem is within the disp_Array function. The idea was that the function should output the variables from objects which are in an array. But unfortunately it doesn't work, in fact, it doesn't output anything.



Therefore I tried to test it by replacing the function's body with just displaying the size of the array, but it prints 0 instead of 4 (the number of elements in that array).



Does that mean that the problem is within the function's parameters and how I pass them? Because on its own, ARRAY_SIZE works very well.







c++ arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 18 at 14:19









John DavisJohn Davis

62




62








  • 3





    You can't copy C-style arrays, and thus can't pass them as parameters. Use std::array or std::vector, which both have a size() member function.

    – Quentin
    Jan 18 at 14:22






  • 4





    Your ARR_SIZE only work with real arrays. Once you pass the array to a function, it will decay to a pointer to its first element (as an argument Painting arr is equal to Painting* arr). And using sizeof on a pointer gives the size of the pointer and not what it points to. My advice is to stop using old simple plain C-style arrays, and instead use std::array or std::vector instead.

    – Some programmer dude
    Jan 18 at 14:23













  • Thank you. I had that feeling that I should do it how it should be done and ease my life, but still I gave it a try. Also there is something strange, when I enter the second element's variables it skips Author. I fixed that with cin.ignore() but I should then press enter, how can I avoid pressing enter and still have that problem fixed?

    – John Davis
    Jan 18 at 14:49











  • @JohnDavis: it would be a different question but lies in the mixing of getline (line oriented) and cin>> (field oriented). After reading a field followed by enter, the end of line is still in input buffer and causes the next getline to return an (almost) empty line. Long story short, dont mix.

    – Serge Ballesta
    Jan 18 at 14:54














  • 3





    You can't copy C-style arrays, and thus can't pass them as parameters. Use std::array or std::vector, which both have a size() member function.

    – Quentin
    Jan 18 at 14:22






  • 4





    Your ARR_SIZE only work with real arrays. Once you pass the array to a function, it will decay to a pointer to its first element (as an argument Painting arr is equal to Painting* arr). And using sizeof on a pointer gives the size of the pointer and not what it points to. My advice is to stop using old simple plain C-style arrays, and instead use std::array or std::vector instead.

    – Some programmer dude
    Jan 18 at 14:23













  • Thank you. I had that feeling that I should do it how it should be done and ease my life, but still I gave it a try. Also there is something strange, when I enter the second element's variables it skips Author. I fixed that with cin.ignore() but I should then press enter, how can I avoid pressing enter and still have that problem fixed?

    – John Davis
    Jan 18 at 14:49











  • @JohnDavis: it would be a different question but lies in the mixing of getline (line oriented) and cin>> (field oriented). After reading a field followed by enter, the end of line is still in input buffer and causes the next getline to return an (almost) empty line. Long story short, dont mix.

    – Serge Ballesta
    Jan 18 at 14:54








3




3





You can't copy C-style arrays, and thus can't pass them as parameters. Use std::array or std::vector, which both have a size() member function.

– Quentin
Jan 18 at 14:22





You can't copy C-style arrays, and thus can't pass them as parameters. Use std::array or std::vector, which both have a size() member function.

– Quentin
Jan 18 at 14:22




4




4





Your ARR_SIZE only work with real arrays. Once you pass the array to a function, it will decay to a pointer to its first element (as an argument Painting arr is equal to Painting* arr). And using sizeof on a pointer gives the size of the pointer and not what it points to. My advice is to stop using old simple plain C-style arrays, and instead use std::array or std::vector instead.

– Some programmer dude
Jan 18 at 14:23







Your ARR_SIZE only work with real arrays. Once you pass the array to a function, it will decay to a pointer to its first element (as an argument Painting arr is equal to Painting* arr). And using sizeof on a pointer gives the size of the pointer and not what it points to. My advice is to stop using old simple plain C-style arrays, and instead use std::array or std::vector instead.

– Some programmer dude
Jan 18 at 14:23















Thank you. I had that feeling that I should do it how it should be done and ease my life, but still I gave it a try. Also there is something strange, when I enter the second element's variables it skips Author. I fixed that with cin.ignore() but I should then press enter, how can I avoid pressing enter and still have that problem fixed?

– John Davis
Jan 18 at 14:49





Thank you. I had that feeling that I should do it how it should be done and ease my life, but still I gave it a try. Also there is something strange, when I enter the second element's variables it skips Author. I fixed that with cin.ignore() but I should then press enter, how can I avoid pressing enter and still have that problem fixed?

– John Davis
Jan 18 at 14:49













@JohnDavis: it would be a different question but lies in the mixing of getline (line oriented) and cin>> (field oriented). After reading a field followed by enter, the end of line is still in input buffer and causes the next getline to return an (almost) empty line. Long story short, dont mix.

– Serge Ballesta
Jan 18 at 14:54





@JohnDavis: it would be a different question but lies in the mixing of getline (line oriented) and cin>> (field oriented). After reading a field followed by enter, the end of line is still in input buffer and causes the next getline to return an (almost) empty line. Long story short, dont mix.

– Serge Ballesta
Jan 18 at 14:54












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255878%2fc-printing-array-of-struct-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255878%2fc-printing-array-of-struct-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre