Loop to save vertices in openGL [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a grid with tetragons and i want to save all the vertices in an array. I wrote ths code:
int counter=0;
int i = 0;
for(i=0; i<=600; i+=40){
verticePosition[counter] = i;
verticePosition[counter+1] = i;
verticePosition[counter+2] = i+40;
verticePosition[counter+3] = i;
verticePosition[counter+4] = i;
verticePosition[counter+5] = i+40;
verticePosition[counter+6] = i+40;
verticePosition[counter+7] = i+40;
counter += 8;
}
I want to save four-four vertices in the table and then i call a function to fill every tetragon with a different color but im getting an error in this for loop:
prog.c:13:1: error: expected identifier or ‘(’ before ‘for’
for(xpos=0; xpox<=600; xpos+=40){
^
and also another error:
prog.c:13:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<=’ token
for(xpos=0; xpox<=600; xpos+=40){
^
I cant find what is wrong with my loop.

The variable xpos is used but not declared, you must declare and initialize it:
for (int xpos = 0; xpos <= 600; xpos += 40) {
Or declare it before the loop:
int xpos;
for (xpos = 0; xpos <= 600; xpos += 40) {

Related

Issue with Float command not working (1st Week of Coding) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
So I am trying to learn C++ and apart of taking basic courses online, I also try to create programs and put my spin on them to learn. I have a very old tutorial that was teaching how to create a battle simulator - I keep getting an error that I can't figure out. The code I have is here:
default_random_engine RandomEngine(time(nullptr));
uniform_real_distribution<float> attack(0.0f, 1.0f);
// Human Properties
float HumanAttack = 0.6f;
float HumanHealth = 250.0f;
float HumanDamage = 200.0f;
float CurrentHumanHealth = HumanHealth
// Skeleton properties
float SkeletonAttack = 0.4f;
float SkeletonHealth = 150.0f;
float SkeletonDamage = 55.0f;
float CurrentSkeletonHealth = SkeletonHealth;
float AttackResult;
int numskeletons;
int numhumans;
I keep getting an error ' Expected ',' or ';' before 'float'
I have been looking things up but as I said - I'm in the first week. Maybe this is to advanced, but I am trying to play with code to parse it out. I appreciate any help.
You have to add semicolon at the end of float CurrentHumanHealth = HumanHealth;
// Human Properties
float HumanAttack = 0.6f;
float HumanHealth = 250.0f;
float HumanDamage = 200.0f;
float CurrentHumanHealth = HumanHealth;
// Skeleton properties
float SkeletonAttack = 0.4f;
float SkeletonHealth = 150.0f;
float SkeletonDamage = 55.0f;
float CurrentSkeletonHealth = SkeletonHealth;
float AttackResult;
int numskeletons;
int numhumans;

Using memcpy to copy a range of elements from an 2d array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Say we have two 2d arrays:
double matrix [64][100];
double array[64][32];
And we want to copy 32 elements from matrix[64][50:82] to array[64][32] using memcpy.
Do you the solution?
double matrix[64][100];
double array[64][32];
for (int i = 0; i < 64; ++i) {
memcpy(&array[i][0], &matrix[i][50], sizeof(double) * 32);
}
However, consider using std::copy() or std::copy_n() instead. They will use memcpy() internally when safe to do so:
#include <algorithm>
double matrix[64][100];
double array[64][32];
for (int i = 0; i < 64; ++i) {
std::copy(&matrix[i][50], &matrix[i][82], &array[i][0]);
or
std::copy_n(&matrix[i][50], 32, &array[i][0]);
}

for loop counter doesn't increment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i'm having some issues with this bit of code. basically "k" doesn't increment more than 1. I've already tried to declare it outside the loop but doesn't fix it. basically what the code does is generating a grid of crystals.
this is a uni assessment and at the moment I'am a newbie with console functions, especially managing the cursor. as you can see, at every iteration i add +2 to pos.x. it seems to work, but when it starts again, pos.x returns to the start value and instead is pos.y to increment(?).
void gridGeneration(Crystal simbols[][Columns])
{
COORD pos = {10, 55};
for (int i = 0; i < Rows; i++)
for (int k = 0; k < Columns; k++)
{
WriteCrystalAt(simbols[i][k].crystal, pos.X, pos.Y, simbols[i][k].color= rand() % light_yellow + light_blue);
pos.X += 2;
if (k = 1)
{
pos.X = 10;
pos.Y += 2;
}
}
}
It would increment further than 1, but you keep setting it to 1 again:
if (k = 1)
You should use == for comparisons.
Your compiler should have issued a warning about this. If it did not, review your warning settings. If it did, stop ignoring warnings.

Error when using constant in defined function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm not a novice, but I may be making a novice mistake here. I'm writing code and I have declared a constant at the top of my program. Whenever I try to use that constant in one of my defined functions, I get an error.
#include <iostream>;
#include <fstream>;
#include <string>;
#include <cmath>;
#define PI 3.14159265358979323846;
#define RADI 300.0;
void CreatePieChart(unsigned char pixels[][WID][DEP], const int dims[3],
double percentages[7], double radius)
{
double radians, distance, deg;
for (int i = 0; i < HITE; i++) {
for (int j = 0; j < WID; j++) {
radians = get_theta(j, i, center);
distance = get_distance_from_center(j, i, center, radians);
deg = quadrant_converter(j, i, center, radians);
if ( RADI < distance ) {
pixels[i][j][0] = 0;
}
}
}
}
When I try to access RADI I get an error.
Syntax Error: ')'
syntax error: missing ';' before '{'
'<' result of expression not used
language feature 'init-statements in if/switch' requires compiler flag '/std:c++17'
All on the same line
Please help.
A #define literally replaces the thing on the left, with the thing on the right.
So when you write
#define RADI 300.0;
if ( RADI < distance )
that is the same as writing
if ( 300.0; < distance )
which has an extra ; in the middle of it. Delete the ;.
I needed to delete the semi-colons after the headers and define declarations

C++ - Unable to change specific vector within a vector<const GLchar*> [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm making a game in opengl and I've had an issue with converting a string to a const char*. Here is my code:
for (GLuint i = 0; i < faces.size(); i++)
{
string fileName(faces[i]),
texture = "../content/skybox/" + fileName + ".jpg";
faces[i] = texture.c_str();
}
Unfortunately once run, the faces[i] just becomes a mess.
You have undefined behaviour:
Texture AssetController::LoadCubeMapTexture(vector<const GLchar*> faces, string ID)
{
for (GLuint i = 0; i < faces.size(); i++)
{
string fileName(faces[i]),
texture = "../content/skybox/" + fileName + ".jpg";
// !!!! texture is a local variable and will be
// destroyed once its scope ends (which is
// on next for iteration, or when for ends).
faces[i] = texture.c_str();
}
the solution is to change interface to return vector of filenames:
Texture AssetController::LoadCubeMapTexture(vector<const GLchar*> faces, string ID,
std::vector<std::string>& facesFileNames)
{
for (GLuint i = 0; i < faces.size(); i++)
{
string fileName(faces[i]);
std::string texture = "../content/skybox/" + fileName + ".jpg";
facesFileNames.emplace_back(texture);
}
As soon as you leave the loop your std::string texture goes out of scope, it's destructor gets called and the memory pointed to by faces[i] becomes invalid.
Just use std::vector<std::string> instead.
On a site note: Don't do using namespace std.