Hi! I have a problem with my code here. When I try to access this scruct array i have, it won’t let me access the actual array space i, but rather it gives me the same array values over and over until it’s done. Do any of you see the error? This is a relatively short except out of my code, the names of the functions are correct, don’t worry about that. However, whenever I try to use draw_boxes, it will only ever give me position 0 of boxes[], the first in the array boxes[]; no more, and thereby not giving out what i want. Please keep in mind that I’m an IT student in his first semester, with no previous experience in programming in c++. Something that might be obvious to you isn’t gonna be as obvious to me.
#include <iostream>
#define CIMGGIP_MAIN
#include "CImgGIP05.h"
using namespace std;
struct Box
{
int x;
int y;
int delta_y;
};
const int box_max = 10, box_size = 40;
void draw_boxes(Box boxes[])
{
white_background();
for (int i = 0; i < box_max; i++)
{
gip_draw_rectangle(boxes[i].x, boxes[i].y, box_size, box_size,
blue);
/* don't mind this, it's something we had in our given header file.
This line is correct*/
}
}
int main() {
Box boxes[box_max] = { 0 };
for (int i = 0; i < box_max; i++)
{
boxes[i].x = i * (box_size + 20) + 10;
boxes[i].y = 0;
boxes[i].delta_y = random(10, 30);
}
draw_boxes(boxes);
return 0;
}
When I use the function draw_boxes(), i want it to cycle through my array of boxes[], and not just use one.
The result I get is a syntax error. It will only ever use a single space of boxes[], even though I give it the indexes for the other spaces, so it only works with one set of values.
Thanks for the help in advance!
std::vector
rather than arrays.– KidKAt
25 mins ago
boxes
contains invoid draw_boxes(Box boxes[])
it will likely only show you the first element. The debugger likely relies on the type ofboxes
to figure out how to display it to you and has no way to deduce the number of elementsboxes
contains from it’s type. Please print out to console the positions of the boxes being drawn.